C Constants

Constants in C refers to fixed values that do not change during the execution of a program. These fixed values are also known as literals. Constants are treated just like regular variables except that their values cannot be modified after their definition. C supports several types of constants which are :


1. Integer Constants
2. Real Constants
3. Single Character Constants
4. String Constants
5. Backslash Character Constant

1. Integer Constants :

A integer constant refers to a sequence of digits. There are three types of integers :

a. Decimal integer : Decimal integer consists of a sequence of digits, 0 through 9, replaced by an optional - or + sign. Valid examples of decimal integer constants are :
 230    -150    980    32768    0    +450
b. Octal integer : An Octal integer constants of any combination of digits from the set 0 through 7, with a leading 0. Some examples of octal integer are :
 037    0    0435    0551
c. Hexadecimal integer : A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A through F. The letter A through F represent the number 10 through 15. Below are some examples of hex integers :
 0x4    0x6A    0xac    0x5b

2. Real Constants : 

The numbers containing fractional parts like 13.05 are called real constants. For example :
 25.07    123.786    -86.75    +0.5
These numbers are shown in decimal notation, having a whole number followed by a decimal point and the fractional part. A real number may also be represented in exponential (or scientific) notation. For example the value 5780.45 may be written as :
 5.78045e3
where e3 means multiply by 103.

3. Single Character Constants :

A Single character constant or simply character constant contains a single character enclosed within a pair of single quote marks. For example :
'A'  'B'  'Z'  '2'  '9'  ':'  '('
Also note that the character constant 'A' is not the same as the character A. The character constant have integer values known as ASCII values. For example, the below C statements
printf("%d", 'A');
would print the number 65, the ascii value of the letter A.

4. String Constants :

A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank spaces. For example :
 "This is String Constant"  "Hello world"  "2018"  "!@#$....??"  "M"
Also note that a character constant is not equivalent to the single character string constant, because a single character string constant does not have an equivalent integer value while a character constant has an integer value.

5. Backslash Character Constants :

C supports some special backslash character constants that are used in output functions. For example, the symbol '\n' stands for newline character. The list of backslash character constants are given below :

Constant Meaning
\a beep sound
\b back space
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\' single quote
\" double quote
\\ backslash
\0 null


Next Topic :