A constant is an identifier whose value can not be changed throughout the execution of a program whereas the variable value keeps on changing. In C there are four basic types of constants. They are:
Integer and Floating Point constants are numeric constants and represent numbers.
Rules to form Integer and Floating Point Constants
Integer Constants
Further, these constant can be classified according to the base of the numbers as:
Maximum values these constants can have are as follows:
| Integer Constant Type | Maximum Value |
|---|---|
| Decimal | 32767 |
| Octal | 32767 |
| Hexadecimal | 32767 |
Unsigned interger constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.
Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.
For example,
50000U decimal unsigned.
1234567889L decimal long.
Floating Point Constants
What is a base 10 number containing decimal point or an exponent. Examples of valid floating point numbers are:
0., 1., 000.2, 5.61123456 etc
Examples of Invalid Floating Point numbers are:
1 decimal or exponent required.
1,00.0 comma not allowed
A Floating Point number taking the value of 5 x 104 can be represented as:
5000. 5e4
5e+4 5E4
5.0e+4 .5e5
The magnitude of floating point numbers range from 3.4E –38 to a maximum of 3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point constants occupy 2 words = 8 bytes.
Character Constants
This constant is a single character enclosed in apostrophes ‘ ’ . For example, some of the character constants are shown below:
‘A’, ‘x’, ‘3’, ‘$’
‘\0’ is a null character having value zero.
Character constants have integer values associated depending on the character set adopted for the computer. ASCII character set is in use which uses 7-bit code with 27 = 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’ have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For example,
‘A’ has 65, blank has 32
ESCAPE SEQUENCE
There are some non-printable characters that can be printed by preceding them with ‘\’ backslash character. Within character constants and string literals, you can write a variety of escape sequences. Each escape sequence determines the code value for a single character. You can use escape sequences to represent character codes:
The following is the list of the escape sequences:
| Character | Escape Sequence |
|---|---|
| " | " |
| ' | ' |
| ? | ? |
| \ | \ |
| BEL | \a |
| BS | \b |
| FF | \f |
| NL | \n |
| CR | \r |
| HT | \t |
| VT | \v |
String Constants
It consists of sequence of characters enclosed within double quotes. For example,
“ red ” “ Blue Sea ” “ 41213*(I+3) ”.