Opposite to variables, constants are those values that do not change throughout the execution of the program. They are also called literals. Like variables, a constant can be of any data type like integer, float, or string.
Note: Constants should be declared using upper case names so that they can be distinguished with the variables.
We can define a constant by any of these two ways –
Const Keyword: This keyword can be used to declare a constant. Memory is allocated to the constant when declared through this method.
Syntax
const datatype constant_name;
Code
#include <stdio.h>
int main()
{
const int LENGTH = 15;
printf("%d", LENGTH);
return 0;
}
Output:
15
We cannot redefine or change the value of a constant.
#include <stdio.h>
int main()
{
const int LENGTH = 15;
printf("%d", LENGTH);
LENGTH = 17; //Error would be generated
return 0;
}
It doesn’t matter whether the const keyword is used before or after the datatype.
const int LENGTH = 15;
is same as
int const LENGTH = 15;
#define pre-processor: There is a preprocessor directive #define using which we can declare a constant.
No memory is allocated to the constant in this approach.
#include <stdio.h>
#define LENGTH 11
int main()
{
printf("%d", LENGTH);
return 0;
}
Output:
11
Constant types in C:
There are two primary categories of constants-
- Numeric Constants
- Character Constants
These two categories are further classified into –
Numeric Constants
- Integer Constants
- Real Constants
Character Constants
- Single Character Constants.
- String Constants
- Backslash Character Constants
Numeric Constants
1. Integer Constants: It is a sequence of digits. Integers can be of three types-
- Decimal Integer
- Octal Integer
- Hexadecimal Integer
Examples-
Decimal Integer-12, -24
Octal Integer-
Hexadecimal Integer- 0x2a
2. Real Constants: They contains fractional values like 19.24.
Character Constants
1. Single Character Constants: They contain a single character enclosed within single quotes (‘ and ‘). It is to be noted that the character ‘5’ is not the same as 5. Character constants having numeric character have a specific value known as ASCII values (American Standard Code for Information Interchange).
For example- ‘A’, ‘t’ etc
2. String Constants: These are a sequence of characters enclosed in double-quotes, and they may include letters, digits, special characters, and blank spaces. It should be noted that “G” and ‘G’ are different. “G” represents a string as it is enclosed within a pair of double quotes whereas ‘G’ represents a single character as it is enclosed within single quotes.
3. Backslash Character Constant: C has some character constants having a backslash in front of them. The lists of backslash characters have a specific meaning which is known to the compiler. They are also called “Escape Sequence”.
For example- \n is used to give a line break.
List of Escape Sequences in C
Escape Sequence | Representation |
---|---|
\0 | Null |
\\ | Backslash |
\” | Double quote |
\’ | Single quote |
\a | Beep sound |
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |