Full Stack Developer
A constant is a value or variable that can't be changed in the program, for example, 10, 20, 'a', 3.4, "c programming" etc.
A constant is a named memory location in a program that holds a single value throughout the execution of that program. It can be of any data type- character, floating-point, string, double, integer, etc. There are various types of constants in C. It has two major categories- primary and secondary constants. Character constants, real constants, integer constants, etc., are types of primary constants. Structure, array, pointer, union, etc., are types of secondary constants.
It can be an octal integer or a decimal integer or even a hexadecimal integer. We specify a decimal integer value as a direct integer value, while we prefix the octal integer values with ‘o’. We also prefix the hexadecimal integer values with ‘0x’.
The integer constant used in a program can also be of an unsigned type or a long type. We suffix the unsigned constant value with ‘u’ and we suffix the long integer constant value with ‘l’. Also, we suffix the unsigned long integer constant value using ‘ul’.
55 —> Decimal Integer Constant
0x5B —> Hexa Decimal Integer Constant
O23 —> Octal Integer Constant
68ul —> Unsigned Long Integer Constant
50l —> Long Integer Constant
30u —> Unsigned Integer Constant
This type of constant must contain both the parts- decimal as well as integers. Sometimes, the floating-point constant may also contain the exponential part. In such a case when the floating-point constant gets represented in an exponential form, its value must be suffixed using ‘E’ or ‘e’.
We represent the floating-point value 3.14 as 3E-14 in its exponent form.
The character constants are symbols that are enclosed in one single quotation. The maximum length of a character quotation is of one character only.
Some predefined character constants exist in the C programming language, known as escape sequences. Each escape sequence consists of a special functionality of its own, and each of these sequences gets prefixed with a ‘/’ symbol. We use these escape sequences in output functions known as ‘printf()’.
The string constants are a collection of various special symbols, digits, characters, and escape sequences that get enclosed in double quotations.
The definition of a string constant occurs in a single line:
“This is Cookie”
We can define this with the use of constant multiple lines as well:
The definition of the same string constant can also occur using white spaces:
“This” “is” “Cookie”
All the three mentioned above define the very same string constant.
Generally, in the C programming language, constants are assigned with some value and this value is known as literal. Hence in the C programming language constants are also known as literals.
const int max =100;
Here “100” is constant integer literal in the above constant expression.
int a = 5;
The below two declarations are not valid for defining the constants:
const int a;
const int a;
In this article, we have discussed the constants in the C programming language. The constants are divided into two categories primary and secondary constants in which primary constants consist of numerical constants like integer, decimal, real, etc, and character constants like character, string, backslash, etc. In secondary constants, we have an array, pointer, structure, union, etc. Constants in C are those whose values cannot be altered once declared and these constants are also known as literals.
Subscribe to get latest updates