Constants and Literals
A constant is a container to store a value. But you can’t change the value of that container during the execution of the program. Thus, the constant is a fixed value, it remains the same through the complete program.
There are two broad categories of constant in C language:
Literal constants are just fixed values that may not change during its execution. Depending of the type of data,there are different types of literals in C language:
- Integer literal constant
- Character literal constant
- Floating point literal constant
- String literals
Integer literal constant
An integer constant is an integer valued number e.g 10, 20, -7, 50 etc.
Thus it consists of a sequence of digits. The Integer constant can be written in three different number system:
- Decimal constant
- Octal constant
- Hexadecimal constant
Decimal constants can consist of any combination of digits from 0 to 9 and if the literal contains two or more digits (means more than ones number), the first digit must be something other than 0.Some valid and invalid decimal literal constants are given below:
Octal constants can consist of a combination of digits from 0 to 7 and the first digit must be 0 to recognize the constant as an octal constant.Some valid and invalid octal constants:
Hexadecimal constants can consist of any combination from 0 to 9 and from A to F( either uppercase or lowercase). A hexadecimal integer must begin with either 0x or 0X.
Note that the letters A to F represent the decimal number 10 to 15 respectively.
Some valid and invalid hexadecimal constants:
Note: We can use integer literal with a suffix of U and L, for unsigned and long, respectively. The suffix can be lowercase or uppercase.
Floating point constants
Floating point constants consist of numbers from 0 to 9 with combination of either a decimal point or an exponent. Some valid and invalid floating point constants:
It should be understood that floating point constants are approximations, whereas integer constants are exact values.
A character constant is a single character, enclosed in apostrophes sign (simply single quotation mark). It will be char type data.
Some valid and invalid character constants:
A String constant consists of any number of consecutive characters( including none) enclosed in double quotes. Some valid string literals are given below:
We can use string with escape sequences:For example:
The compiler automatically places a null character at the end of every string in C language. This is not visible when the string is displayed; it is represented by ‘/0’.
A symbolic constant is a name that refers to a sequence of characters. It can be a numeric constant or a character constant or a string constant. Symbolic constants can be defined by the user.
In C language, there are two ways to define symbolic constant.
- Using # define preprocessor
- Using const keyword
Syntax for # define preprocessor -:
When a program is compiled each occurrence of symbolic constant # define is replaced by its corresponding data.
Let us understand this through a program:
In the above program, during the compilation CONSTANT will be replaced by its value which is 45.
Symbolic constant # define usually defined at the beginning of the program and the definition do not end with semicolon.
Syntax for const keyword -:
We have to use const as a prefix to declare a constant in c language. For example :
The above given program will raise an error called “assignment of read-only variable `v`.