
C Constants and Literals | C Programming Tutorial
A constant is fixed a value, which cannot change the meaning of value during program execution. These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.
The constants are treated actually like standard variables aside from that their qualities can’t be altered after their definition.
It is considered best practice to define constants using only upper-case names.
Constant Types in C
In C Constants are categorized into two basic types and each of these types has its sub-types. Those are:
- Numeric Constants.
- Integer Constants.
- Real / Floating Point Constant.
- Character Constants.
- Single Character Constants.
- String Constants.
- Backslash Character Constants.
Numeric Constants | C Constants and Literals
Integer Constants
An integer Constant can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
An integer literal can also have a suffix that is a combination of U and L for unsigned and long respectively. The suffix can be uppercase or lowercase and can be in any order.
Here are some Legal and Illegal examples of Integer Constants
- 215u – /* Legal */
- 212 – /* Legal */
- 215u – /* Legal */
- 0xFeeL – /* Legal */
- 078 – /* Illegal: 8 is not an octal digit */
- 032UU – /* Illegal: cannot repeat a suffix */
Following are some other examples of various types of Integer Constants
- 85 – /* decimal */
- 0213 – /* octal */
- 0x4b – /* hexadecimal */
- 30 – /* int */
- 30u – /* unsigned int */
- 30l – /* long */
- 30ul – /* unsigned long */
Real / Floating Point Constants
A Floating-Point Constant has an integer part, a decimal point, a fractional part and an exponent part. You can represent floating point literals either in decimal form or exponential form.
While representing using decimal form, you must include the decimal point, the exponent or both and while representing using exponential form you must include the integer part, the fractional part or both. The signed exponent is introduced by e or E.
Following some Legal and Illegal examples of Floating-Point Constants
- 3.14159 /* Legal */
- 314159E-5L /* Legal */
- 510E /* Illegal: incomplete exponent */
- 210f /* Illegal: no decimal or exponent */
- .e55 /* Illegal: missing integer or fraction */
Character Constant | C Constants and Literals
Single Character Constant
It simply contains a single character enclosed within a pair of single quote (‘ and ‘). It is to be noted that the character ‘8’ is not the same as 8. Character constants have a specific set of integer values known as ASCII values (American Standard Code for Information Interchange).
Here are some examples of Single Character Constants
‘a’
‘8’
‘>’
String Constants
String literals or constants are enclosed in double quotes (” and “). A string contains characters that are similar to character literals: plain characters, escape sequences and universal characters.
It is again to be noted that “E” and ‘E‘ are different – because “E” represents a string as it is enclosed within a pair of double quotes whereas ‘E’ represents a single character.
You can break a long line into multiple lines using string literals and separating those using whitespaces.
Examples of string literals. All the three forms are identical strings.
“Welcome Ekvij!”
“String, /
Constants”
“58 + 23”
Backslash Character Constants
C supports some character constants having a backslash in front of it. The lists of backslash characters have a specific meaning which is known to the compiler. They are also termed as “Escape Sequence”. All Escape Sequences are Single Character Constants. They are used to represent like newline (\n) or tab (\t). Here, you have a list of some of such escape sequence codes:
Escape Sequence | Meaning |
\t | Tab |
\n | New Line |
\a | Alert / bell / Beep Sound |
\b | Backspace |
\f | Form Feed |
\r | Carriage Return |
\v | Vertical Tab |
\’ | Single Quote |
\” | Double Quote |
\\ | Backslash |
\0 | Null |
\ooo | Octal number of one to three digits |
\xhh . . . | Hexadecimal number of one or more digits |
Following is the example to show few escape sequence characters
#include<stdio.h>
int main()
{
printf(“Hi \n Welcome\t to EKVIJ.COM”);
return 0;
}
For the above code output as follows:
Hi
Welcome to EKVIJ.COM
Defining Constants
There are two simple ways in C to define constants:
1. Using #define preprocessor.
2. Using const keyword.
The #define Preprocessor
The definition of #define pre-processor is as follows:
Syntax: #define identifier value
Example 1: #define GRAVITY 9.81
Example 2: #define NEWLINE ‘\n’
The const Keyword
You can use const prefix to declare constants with a specific type as follows:
const type variable = value;
Example 1: const float Pi=3.14;
Example 2: const char TAB= ‘\t’;