Constants in C language refer to fixed value that cannot change during the execution of a program. C supports several types of constants:

Numeric constants
Number constants consist of a digits. A numeric constants have no decimal point( . ). There are the rule of for defining numeric constants:
1. Numeric constant must have at least one digit.
2. Not allowed comma or space within the numeric constant.
3. In case numeric constant is positive or negative but default sign is always positive.

Integer constants
An integer constant refers to a sequence of digits. The sequence of digits are a three types of integer Decimal integer, Octal integer and hexadecimal integer.

Decimal integer is consist a set of digits 0,1,2,3,4,5,6,7,8,9 (base 10).

Some valid example of decimal integer constants are:
Some invalid example of decimal integer constants are:
07654                First digits cannot be zero
45.34                (.) is illegal character
56#54                (#) is illegal character
67 6                 Blank space not allowed
67,6                 Comma not allowed

Octal integer is consist only combination of digits 0,1,2,3,4,5,6,7 (base 8). In octal integer constants, a first digits must be 0.

Some example of octal integer constants are:

Hexadecimal integer is formed by combination of digits 0,1,2,3,4,5,6,7,8,9 and letter A,B,C,D,E,F or a,b,c,d,e,f (base 16).In hexadecimal integer constants, a first character should be 0x or ox.

Some example of hexadecimal constants are:

By default types of integer constant is int. If the value of integer constant exceeds the range of value in type of int (unsigned int or long int). we can also mention the type of constant with l or L (for long), u or U (for unsigned), ul or UL (for unsigned long).

7634                   Integer constant of type int
6763u or 7663U         Integer constant of type unsigned int
3425ul or 3425         Integer constant of type unsigned long int

Real (floating point) constants
Floating point constants are numeric constants that contain decimal point or fractional part.

Some valid floating point are:

There constant can be written in the mantissa and exponent form, which are separated by e or E. The mantissa can be an integer or real number and the exponent can be only an integer. For example number 3400000 can be written as 3.4e6, here mantissa is 3.4 (the part appearing before e) and exponent is 6 (the part appearing after e).

By default the type of a floating point constant is double. We can mention the type of constant with f or F (for float type), l or L (for long double).

For example
4.6e8              floating point of type double
6.4f or 6.4F   floating point of type float
4.5l or 4.5L   floating point of type long double

Character constants
A character constant is one character enclosed with in single quotes ( ‘ ).

Some valid character constant are:
'4'        'r'       '%'     'G'      '#'
Some invalid character constant are:
'the'     only one character within quotes
"f"       Double quotes are not allowed 
h         Single quote missing

Every character constant has unique integer value associated with it. This integer is the numeric value of the character in the machine character code. The machine is using American Standard Code for Information Interchange (ASCII). The character A represent integer value 65.

Some ASCII values are:
A-Z ASCII value (65-90)
a-z ASCII value (97-122)
0-9 ASCII value (48-57)

String constants
A string constant is enclosed within double quotes ( ” ). At the end of string, the null character ( \0 ) is automatically placed by the compiler.

Some example of string constants are:

Note “A” and ‘A’ are different, the first one is a string constant which consists of character A and ( \0 ). The second one is a character constant which represents integer value 65 (in ASCII).

Leave a Reply