Professional Documents
Culture Documents
Chapter - 3
1
3.1 Constants
The quantity which does not change
during the execution of a program is
known as constant
2
C Constants
C Constants
Numeric Non-numeric
Constants Constants
3
Integer Constant
An integer constant is a whole number
It is a sequence of digits without decimal
point
It is prefixed with a plus or minus sign
The general form of an integer constant is,
Sign digits
Where,
Sign an optional plus sign for positive
numbers or minus for negative numbers
Digits a sequence of digits
Ex: 123, -321, 0, 654321, +78
4
Floating constant
A floating constant is a number with a
decimal point
It is defined as a sequence of digits
followed by a decimal point
They may be prefixed by plus or minus
sign
Ex: -243.09, +27.23, 72.3
5
The general form is
Sign integer decimal fractional
part part part
Where,
sign an optional plus or minus
Integer part a sequence of digits before a decima
point
Decimal point period symbol
Fractional part a sequence of digits after a
decimal point
6
Character constant
A character constant is a single character
enclosed within a pair of apostrophes
Ex: ‘a’, ‘#’, ‘5’
7
String constant
A string constant is a sequence of
characters enclosed within a pair of
double quotes
Ex: “hai”, “500”, “a+b”
8
3.2 Variables
A variable is a value that can change any
time.
It is a memory location used to store a
data value.
Examples of variable names are
Sun, number, Salary, Emp_name,
average1
9
Rules for naming variables :
They must always begin with a letter,
although some systems permit underscore
as the first character.
Keywords, special characters and White
spaces are not allowed
Variable names are case sensitive
Always choose an appropriate variable
name that makes proper sense to the user
1
0
Valid variables Invalid variables
marks 8ab
arr[20] area_ _of_ _ circle
Area_of_circle() gross-sal
1
1
Declaration of variables
All the variables must be declared before
they are used in the program
The declaration does two things.
◦ Tells the compiler the variables name.
◦ Specifies what type of data the variable will
hold.
1
2
The general format of any declaration is
1
3
3.3 Assigning values to variables
Each variable is associated with one or
more values
The process of giving values to variables
is called assignment of values
1
4
The general form is
variable_name = value ;
where
variable_name represents the memory
location where the value is stored
= assignment operator
value constant or variable
1
5
3.4 Data types
Data types indicate the type of data that a
variable can hold
The data may be numeric or non-numeric
in nature
1
6
Datatypes are categorized as
C Datatypes
int
float char
double void
1
8
TYPE KEYWORD SIZE(in bytes)
Integer int 2
Real(floating-point) float 4
Character char 1
Non-specific void --
1
9
Type int
Thisis a keyword used to indicate an integer
number
int num;
Any integer number is a sequence of digits
without a decimal point
The range depends on the word length of the
computer
Word length means the number of bits that can
be accessed at a time by processor
16 bits (2 bytes) of memory
2
0
Valid Examples Invalid Examples
-248 3,333
14042 -34.0
27246 +3,468.3
0 99999999
+1996
2
1
Type float
This is a keyword used to indicate an
floating-point numbers(real numbers)
float num;
Precision of upto 6 digits
32 bits (4 bytes) of memory
Examples: 23.05, 56.5, 32
2
2
Valid Examples Invalid Examples
-248.45 3,333
1.4042 E + 02 -34.0
27246 +3,468.3
0 99999999
2
3
Type char
This is a keyword used to indicate the
character type data
char gender;
gender='M';
8 bits (1 byte) of memory
A character constant is enclosed within a
pair of apostrophes
Examples: ‘a’, ‘m’, ‘$’ ‘%’ , ‘1’, ‘5’
2
4
A string constant is sequence of
characters enclosed within a pair of
quotes. It is terminated by a null
character.
Examples: “Program” , “2k8”
2
5
Type double
This is a keyword used to indicate a double
precision floating-point number
double num;
64 bits (8 bytes) of memory
The precision is associated with accuracy of
data
Float stores maximum of 6 digits after decimal
point but double stores 16 digits after decimal
point
Ex: 123.00000000000000000
2
6
Type void
Stores nothing
Indicates the compiler that there is
nothing to expect
2
7
Data Types and their range
Type Approximate Minimal Range
Size in Bits
char 8 -128 to 127
unsigned 8 0 to 255
signed char 8 -128 to 127
int 16 -32,768 to 32,767
3
0
Backslash Meaning
constants
'\a’ Audible Alert (Bell)
'\b' Backspace
'\f’ Form feed
'\n’ New Line
'\r' Carriage Return
'\t' Horizontal tab
'\v’ Vertical Tab
'\'' Single Quote
'\?‘ Double Quote
'\?‘ Question Mark
'\\' Back Slash
3 '\0' Null
1
3.6 Data type modifiers
The basic data types except void can be
modified using a series of type modifiers
To have an accuracy for a real number, we are
using double data type
Similarly, for integer data, the accuracy can be
obtained by some data modifiers
These modifiers are also called qualifiers
They are
◦ Signed
◦ Unsigned
◦ Short
3 ◦ Long
2
signed
◦ This is applied to int.
◦ The default declaration assumes a signed
number. If it is unsigned it can hold integers 0
to 255.
◦ If it is signed it hold the numbers in the range
-128 to 127
unsigned
◦ This can be used for both int and char
◦ It can also be used in combination with long
or short
◦ It can be used to create an unsigned integer
3
3
long
◦ This is used with int and double datatype
◦ It doubles the length in terms bits
◦ If an int is of 16-bits, then long int is of 32-
bits
short
◦ This makes the size of an int half
◦ Size of the short int is that of the int
3
4
3.7 Declaring variables as constant
The values of some variable may be
required to remain constant through-out
the program.
This is done by using the qualifier const
at the time of initialization.
Example:
const int class_size = 69;
3
5
Declaring variables as volatile
A volatile variable is the one whose
values may be changed at any time by
some external sources.
Example:
volatile int num;
3
6
3.8 Symbolic constants
A symbolic constant value can be defined as a
preprocessor statement and used in the
program as any other constant value.
3
7
Advantages
Modifiability
If we want to change the value of
“pi” from 3.14 to 3.14159 to improve
accuracy or the number 50 to 100 to
increase the number of persons, it is easy
to do
Understandability
When the same value means
different things in different places, it is
made clear using symbolic constants
3
8
Examples of Symbolic Constants
Valid Invalid
#define X 25 #define X=25
#define PASS_MARK # define X 25
50 #define X 25;
#define PI 3.14159 #define X 25,Y 35
#define NAME “NTTF” #Define X 25
#define $ 25
3
9
3.9 Delimiters
They limit the boundary between the
basic elements of a program
They are also called as separators
They separate constants, variables and
statements
Comma, semicolon, blankspace,
apostrophes, double quotes are commonly
used delimiters
4
0
3.10 Multiple Assignment statement
The process of giving initial values to
variables is known as initialization
More than one variable can be initialized
in a single statement
Ex:
int x = y = z = 10;
4
1
3.11 Statement
It is an instruction to the computer to
perform specific operation
It may be a declaration, an input-output
action, arithmetic and logical operation,
assignments or control statements
4
2