In C programming, a constant is a fixed value that does not change during the execution of a program. C provides several types of constants, which can be classified according to the kind of value they represent.
The major types of constants in C are:
An integer constant represents a whole number without a fractional or decimal part. It may be positive, negative, or zero.
10
250
-50
0
For example, in int age = 25;, the value
25 is an integer constant.
Floating-point constants, also called real constants, represent numbers that contain a fractional part. They are normally written with a decimal point or in scientific notation.
3.14
-0.75
25.50
6.02e23
For example:
float pi = 3.14f;
Here, 3.14f is a floating-point constant.
An octal constant is an integer constant written using the
base-8 number system. Octal digits range from
0 to 7. In C, an integer literal beginning with
0 is interpreted as octal when it is not followed by
x or X.
012
075
0100
For example, 012 is an octal integer constant. Its decimal
equivalent is 10.
A hexadecimal constant is an integer constant written using
the base-16 number system. It uses the digits
0-9 and the letters A-F or a-f.
Hexadecimal constants begin with 0x or 0X.
0x10
0xFF
0X2A
For example, 0x10 represents the decimal value
16.
A character constant represents a single character and is enclosed within single quotation marks.
'A'
'b'
'7'
'@'
For example:
char grade = 'A';
Here, 'A' is a character constant.
A string literal is a sequence of characters enclosed
within double quotation marks. In C, a string literal is
stored as an array of characters terminated by a null character
('\0').
"Hello"
"Welcome to C"
"12345"
For example:
printf("Hello World");
Here, "Hello World" is a string literal.
Escape sequences are special character sequences that begin
with a backslash (\). They are used to represent special
characters or actions that cannot always be written directly in a string or
character literal.
| Escape Sequence | Meaning |
|---|---|
\n |
New line |
\t |
Horizontal tab |
\b |
Backspace |
\r |
Carriage return |
\\ |
Backslash character |
\' |
Single quotation mark |
\" |
Double quotation mark |
\0 |
Null character |
For example:
printf("Hello\nWorld");
Here, \n moves the cursor to the next line.
| Type | Example | Description |
|---|---|---|
| Integer constant | 100 |
Whole-number value |
| Floating-point constant | 3.14 |
Decimal or real value |
| Octal constant | 075 |
Base-8 integer value |
| Hexadecimal constant | 0xFF |
Base-16 integer value |
| Character constant | 'A' |
Single character |
| String literal | "Hello" |
Sequence of characters |
| Escape sequence | \n |
Represents a special character or action |
Therefore, C constants can represent numeric values, characters, strings, and special character sequences. Understanding these different types is important because they are frequently used in expressions, assignments, input/output operations, and other C programs.