C Constants
Constants are values that do not change during the execution of a program. In C, constants can be defined using the const
keyword. Constants are used to represent values that are known at the time of writing the code and do not need to be changed during the execution of the program.
There are two types of constants in C: Numeric constants and Character constants.
Numeric Constants
Numeric constants can be either integers or floating-point numbers. Integer constants are whole numbers without a fractional component, while floating-point constants have a fractional component. Numeric constants can be represented in decimal, octal, or hexadecimal format. For example, the following are valid numeric constants:
1123 // decimal constant 20123 // octal constant 30x123 // hexadecimal constant 412.34 // floating-point constant
Character Constants
Character constants are single characters surrounded by single quotes. For example, the following are valid character constants:
1'A' 2'1' 3'+'
Declaring Constants
To declare a constant in C, the const
keyword is used in conjunction with the data type of the constant. For example, to declare a constant integer named MAX_COUNT
with the value of 100, the syntax would be:
1const int MAX_COUNT = 100;
Note that once a constant has been declared, its value cannot be changed during the execution of the program.
Using Constants
Constants are used in C programs in the same way as variables. For example, to output the value of a constant integer, the syntax would be:
1printf("The value of the constant is: %d", constant_name);
They represent values that are known at the time of writing the code and do not change during the execution of the program. Constants can be either numeric or character and are declared using the const
keyword in conjunction with the data type of the constant.