Constants can be defined as the values that cannot be modified or altered by a program. Constants can be numbers, characters or strings. They are named as integer constants, floating point constants, character constants and string constants. A constant can either be a literal constant or a symbolic constant.
A literal constant is the constant value which is typed directly into the source code whenever it is needed. Literal constants are well-known as ‘constants’. Likewise, a symbolic constant is the constant that is named. Such a constant has a fixed value that cannot be modified during the program execution phase.
Constants make it easy to define variables which cannot be modified by any other part in the code. In C, a constant can be defined by placing the keyword
const in front of any variable declaration or by using the symbolic constant using the
define macro. If the keyword
volatile is kept after
const, then external routines like hardware devices can modify the variable. There are four types of constants in C Programming, they are:
- Numeric Constants
- Octal and Hexadecimal Constants
- Character Constants
- String Constants
- Numeric Constants: The numeric constants are the ones with numeric values like
doubleetc. Integer constants are the numbers lacking fractional components. An integer literal can be a decimal, octal or hexadecimal constant. A prefix specifies the base or radix of the constant value, i.e. 0x or 0X for hexadecimal constant, 0 for octal and no prefix for decimal constant.
3u /*unsigned int*/
3l /*long int*/
3ul /*unsigned long int*/
For instance, 100 and -100 are integer constants. Floating point constants require the decimal point followed by the number’s fractional part. For example, 12.134 is a floating point constant. C also provides the functionality of using scientific notation for floating point number. Floating point constants written with a decimal point are represented by the C compiler as double precision numbers. The following rules apply to all the numeric type constants:
- Commas and blank spaces are not allowed to be included within the constants.
- A minus (-) sign can precede the constant, if desired. The minus sign is an operator that changes the sign of a positive constant, although it can be considered as a part of the constant itself.
- The value of a constant can never exceed the specified minimum and maximum bounds. For each type of constant, these bounds may be different from one C compiler to another.
- Octal and Hexadecimal Constants: Octal number system is the system which is based on 8 and it uses the digits ranging from 0 to 7. In octal number system, the number 10 is the same as 8 in decimal.Hexadecimal number system is the system with base 16. It uses the digits from 0 to 9 and the letters from A to F.In C programming, prefixes are used to indicate the base of the numbers that are being used as constants. A number with prefix 0x or 0X indicates a hexadecimal value. Likewise, a number with prefix 0 indicates an octal number. For example, 16 in decimal number system is written as 0x10 or 0X10 in hexadecimal number system and 020 in octal number system. Let us take a look at the following program that displays the decimal, octal and hexadecimal equivalent values of a decimal number:
int x = 100;
printf("Decimal = %d; Octal = %o; Hexadecimal = %x\n", x, x, x);
printf("Decimal = %d; Octal = %#o; Hexadecimal = %#x\n", x, x, x);
The output of the program written above is:
Decimal = 100; Octal = 144; Hexadecimal = 64
Decimal = 100; Octal = 0144; Hexadecimal = 0x64
- Character Constants: A character constant is represented by a single character enclosed within a pair of apostrophes. Character constants have integer values that are determined by the computer’s specific character set. ASCII is one of such character sets and it is widely used by most of the computers. In ASCII, each individual character is numerically encoded with its own unique combination of 7 bits. For example, the ASCII value of character ‘A’ is 65 and ‘a’ is 97.
- String Constants: A string can simply be defined as a set of characters that is enclosed by a pair of double quotes. An example of a string is
"I love programming". String constants contain any number of consecutive characters written within two double quotation marks. A character constant ‘A’ and a single character string constant “A” are not equivalent.