C Constants & Variables
Variables
an entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants.
In any language, the types of variables that it can support depend on the types of constants that it can handle. This is because a particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant.
The rules for constructing different types of constants are different. However, for constructing variable names of all types the same set of rules apply. These rules are given below.
Rules for Constructing Variable Names
A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort.
The first character in the variable name must be an alphabet or underscore.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
Constants
Constants refer to fixed values that do not change during the execution of a program. Figure below shows the various types of constants available in C :
Integer Constants
Integer constants are whole numbers without any fractional part.
Rules for constructing integer constants are :
- An integer constant must have at least one digit and must not have a decimal point.
- An integer constant may be positive or negative. If no sign is preceded an integer constant, it is assumed to be positive.
- Embedded spaces, commas and non-digit characters are not allowed.
- An integer constant must have at least one digit.
- It must not have a decimal point.
- No commas or blanks are allowed within an integer constant.
- The allowable range for integer constants is -32768 to 32767.
There are three types of integer constants namely :
(i) Decimal
(ii) Octal
(iii) Hexadecimal
(i) Decimal Integer Constants
Decimal integer constants can consist any combinations of digits from 0 to 9, preceded by an optional + or – sign.
Valid examples are :
0 +47 179 -240 22099
Invalid examples are :
Invalid Decimal Integer Constants |
Reason For Invalidity |
15 750 |
Blank space is not allowed |
20,000 |
Comma is not allowed |
$1000 |
$ symbol is not allowed |
12.55 |
Decimal point is not allowed |
(ii) Octal Integer Constants
Octal integer constants can consist of any combinations of digits from 0 to 7. However, the first digit must be zero (0), in order to identify the constant as an octal number. For instance, decimal integer 8 will be written as 010 as octal integer.
Valid examples are :
0 047 0179 0240
(iii) Hexadecimal Integer Constants
Hexadecimal integer constants can consists of any combination of digits from 0 to 9 and letters from A to F (either uppercase or lowercase). A hexadecimal integer constant must begin with either 0x or 0X. For instance, decimal integer 12 will be written as 0XC as hexadecimal integer.
Valid examples are :
0x 0X2 0x7A 0xbcd
Thus, the number 12 will be written either 12 (as decimal), 014 (as octal) and 0xC (as hexadecimal).
Note : The larger integer value that can be stored is machine dependent.
Machine Type |
Largest Integer Value |
16 bit machines |
32,767 |
32 bit machines |
2,147,483,647 |
Real Constants
Real constants are also called as floating point constants. Real constants are numbers having fractional parts. These may be written in one of the two forms :
(i) Fractional form (ii) Exponential form
(i) Fractional Form
The fractional form consists of a series of digits representing the whole part followed by a decimal point and series of digits representing the floating part. The whole part or fractional part can be omitted but not both. The decimal point cannot be omitted.
Rules for constructing real constants expressed in fractional form are :
- A real constant must have at least one digit and it must have a decimal point.
- A real constant could be either positive or negative (default sign is positive).
- Embedded spaces, commas and non-digit characters are not allowed.
Valid examples are :
0.0002 -0.96 179.47 +31.79
+.2 -.47 179. .99
Invalid examples are :
Invalid Fractional Form |
Reason For Invalidity |
12,000.50 |
Comma is not allowed |
31 |
No decimal point |
12.30.45 |
Two decimal point |
$1000.75 |
$ symbol is not allowed |
15 750.25 |
Blank space is not allowed |
(ii) Exponential Form
The exponential (or scientific) form of representation of real constants is usually used if the value of the constant is either too small or too large. In exponential form of representation, the real constant is represented in two parts namely, mantissa and exponent. The syntax is :
mantissa e exponent [or]
mantissa E exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separating the mantissa and exponent can be written in either lowercase or uppercase.
For example, 3.5 can be written as 0.35 x 10 = 0.35E1 where the mantissa part is 0.5 (the part appearing before E) and the exponent part is 1 (the part appearing after E).
Rules for constructing real constants expressed in exponential form are :
- The mantissa and the exponent should be separated by a letter e (either uppercase or lowercase).
- The mantissa and exponent part may have a positive or negative sign (default sign is positive).
- The mantissa and exponent part must have at least one digit.
- Embedded spaces, commas are not allowed.
Valid examples are :
0.31e2 12e-2 3.1e+5
3.18E4 -1.2E-2
Invalid examples are :
Invalid Exponential Form |
Reason For Invalidity |
12,000e2 |
Comma is not allowed |
3.1e 2 |
Blank space is not allowed |
3.1E+2.4 |
Exponent must be an integer |
Note : Floating point constants are normally represented as double precision quantities. However, the suffixes f or F may be used to force single precision and l or L to extend double precision further.
Single Character Constants
A single character constant or a character constant consist of a single character encloses within apostrophes.
Rules for constructing single character constants are :
- A character constant is a single alphabet, a single digit, a single special symbol or a blank space.
- The maximum length of a character constant can be one character.
Valid examples are :
‘B’ ‘V’ ‘+’ ‘$’ ‘ ’
Invalid examples are :
‘abc’ ‘123’
String Constants
A string constant is a sequence of characters encloses within double quotation marks. Technically, a string is an array of characters. The compiler automatically places the null character (\0) at the end of each such string, so program can conveniently find the end.
Rules for constructing string constants are :
- The string constant may be letters, numbers, special characters and blank spaces.
- Every string constant ends up with a null character, which is automatically assigned.
Valid examples are :
“India” “2002” “WELCOME”
“*.*” “B” “$120”
Note :
- A character constant ‘B’ is not the same as the string constant that contains the single character “B”. The former is the single character and the latter a character string consisting of character B and \0 (null).
- A character constant as an equivalent integer value, whereas single character string constant does not have an equivalent integer value. It occupies two bytes, one for the ASCII code of B and another for the null character with a value 0, which is used to terminate strings.
VARIABLES
A quantity, which may vary during the execution of a program, is called as variable. It is a data name that may be used to store a data value. A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.
Rules for constructing a variable are :
- A variable name is any combination of alphabets, digits or underscores.
- The first character in the variable name must be an alphabet.
- No comma or blank spaces are allowed within the variable name.
- No special characters other than an underscore (_) can be used in a variable name.
- A variable name cannot be a keyword.
- Variable names are case sensitive, i.e., uppercase and lowercase letters are treated as distinct and different. Hence, MARK, mark and Mark are three separate names.
- A variable name should not be of length more than 31 characters.
Valid examples are :
AVERAGE height subject1 n1
City male_sum fact n
Invalid examples are :
Invalid Variable Name |
Reason For Invalidity |
char |
char is a keyword |
price$ |
$ sign is illegal |
group one |
Blank space is not allowed |
786 |
First character must be an alphabet |
Note :
- Some compilers permit underscore as the first character.
- There is no limit on the length of the variable, but some compilers recognize only first eight characters. For example, the variable name dataname1 and dataname2 mean the same thing to the compiler.
DECLARATION OF VARIABLE
data_type V1, V2, . . . Vn ;