A constant is an entity whose value remains the same throughout the execution of the program. It can not be placed on the left side of an assignment operator. It can only be placed on the right side of the assignment operator.
Types of constants in C language
In c Constant are classified as:
- Literal constants
- Qualified constants
- Symbolic constants
1) Literal Constants:
A literal constant denotes a fixed value, which may be an integer, floating-point number, character, or string. The type of literal constant is determined by its value.
Literal constants are of the following types:
- Integer literal constant
- Floating point literal constant
- Character literal constant
- String literal constant
A.Integer literal constant: Integer literal constants are integer values like 1, 2, -1, 100, 1400, etc. The rules for writing integer literal constants are as following:
- An integer literal constant must have at least one digit.
- It should not have any decimal point.
- It can be either positive or negative.
- No special characters and blank spaces are allowed within an integer literal constant.
- It can be end with u or l modifiers like 100u, 2111111144L, etc.
B.Floating point literal constant: Floating-point literals are values like 45.33, 97.8, -3.33, .222224, -442.33, etc. Floating-point literal constants can be written in fractional or exponential form. The rules for writing floating-point literal constants are as following:
- A fractional floating-point literal constant must have at least one digit.
- It should have a decimal point.
- It can be either positive or negative.
- No special characters and blank spaces are allowed within a floating-point literal constant.
- A floating-point literal constant by default is assumed to be of type double. If you suffix a floating-point number using f or F for example 23.44f, then it is assumed to be of type float.
C.Character literal constant: A character literal constant can have one or at most two characters enclosed within single quotes e.g. ‘a’, ‘x’, ‘\n’, ‘\t’, etc. Character literal constant can be classified as:
A. Printable character literal constant
B. Non-printable character literal constant.
A.Printable character literal constant: All the characters which are printable except quotation marks, backslash, and enclosed within single quotes are called printable character literal constants. For example ‘a’, ‘B’, ‘x’, ‘#’, etc.
B.Non-Printable character literal constant: Non-printable character literal constants are represented with the help of escape sequences. For example \n, \t, \a, \r, \b, etc.
2) Qualified constants:
Qualified constants are created using a const qualifier. The following statement creates a qualified integer constant named a:
const int a=20;
we can not change the value of qualified constants, it remains the same throughout the program. In the above example if we try to change the value of constant into it is not changed and produces an error.
3) Symbolic constants:
Symbolic constants are created with the help of a defined preprocessor directive. For example, #define PI 3.14 defines PI as a symbolic constant with value 3.14. Each symbolic constant is replaced by its actual value during the preprocessing stage.