The values cannot be changed during the execution of program are called constants. The fixed values are also called literals.
Types of Constants ( Literals )
Integer Constants
Integer Constants are numbers that do not have a decimal point or an exponential part.They can be represented as:
Decimal Number | 0 to 9 | Ex: 7, 8, 6, -80, 786, etc., |
Octal Number | 0 to 7 | EX: 0, 012, 052, etc., |
Hexadecimal Number | 0 to 9, A, B, C, D, E, F | Ex: 0x786, 0xA123, 0x2b, etc., |
Float Constants
Float Constants are numbers that have a decimal point or an exponential part.They can be represented as:
Float Constant | Value |
7.867e3 | 7,867 |
4e-11 | 0.00000000004 |
1e+5 | 100000 |
7.e10 | 60000000000 |
0.16 | 0.16 |
Character Constants
Character Constants contains a single character enclosed within a single quotation mark symbol.Ex: 'M', 'A', '9', '-', etc.., .
String Constants
A String Constants contains a sequence of characters or escape sequences enclosed in double quotation mark symbols.EX:"MDSN", "Learn C\n", " ", etc.., .
How to Define a Constant in C
There are two simple ways to define Constants.- #define ( Preprocessor)
Example:
#include <stdio.h> #define Length 10 //Preprocessor #define Width 5 //Preprocessor int main(){ int Area; Area=Length*Width; printf("Area=%d",Area); return 0; }
- const ( Keyword )
Example:
#include <stdio.h> int main(){ const Length=10, Width=5; // Keyword int Area; Area=Length*Width; printf("Area=%d",Area); return 0; }