Back to: C Programming for Beginners
In this lesson, we will talk about constants in C programming. Constants sometimes referred to as literals, are entities that do not change, while variables are entities that can change. Constants are fixed values used in a program and their value remains the same during the entire execution of the program. We will focus on constants, their types and rules for creation in this lesson, and discuss variables in the next lesson.
Table of Contents
Types of Constants
There are mainly two types of constants in C language.
- Primary Constants
- Secondary Constants
Both of these can be further divided into sub-types as mentioned below:
In this particular lesson, we will focus on primary constants. We will discuss secondary constants in detail later.
Numeric Constants
These are digits that may or may not have decimal points depending upon their sub-type as mentioned below.
- Integer Constants
- Floating Point or Real Constants
There are some rules for creating Numeric Constants[1].
- They must have at least one digit.
- Space, commas or any other special symbols are not allowed.
- They can be both positive and negative numbers. If no sign is mentioned, then by default positive is assumed.
Integer Constants
Integer constants are digits that do not have a decimal point (.
). They can be written using either decimal numbers, octal numbers or hexadecimal numbers.
Decimal Constants
They contain digits from 0
to 9
and can not begin with a zero.
1 |
54, 69, 1234, -239 |
Octal Constants
They contain digits from 0
to 7
and must begin with a zero.
1 |
054, 069, 01234, 04000 |
Hexadecimal Constants
They contain digits from 0
to 9
and letters from a
to f
(both upper and lower cases are allowed). They must also begin with either 0x
or 0X
.
1 |
0x69, 0XFF, 0x54A, 0X12b |
The C language also allows mixing all three sub-types, i.e. decimal, octal, and hexadecimal, into one variable or constant. Octal and hexadecimal types are commonly used while writing low-level programs.
1 |
int i = 0x69 + 054 + 13; |
By default, the integer constant is of type int
and depending upon the operating system, takes up either 2 or 4 bytes of memory. If the value of an integer constant is too large to fit into int
then the type constant is promoted to long int
. If the value is still too large, then it is promoted to unsigned long int
.
We can explicitly specify the type of an integer constant as long int
in the code by appending l
or L
at the end of the constant.
1 |
54l, 45L, 069l, 096L, 0xFFl, 0Xa2L; |
We can do the same for unsigned int
by appending u
or U
at the end of the constant.
1 |
54u, 45U, 069u, 096U, 0xFFu, 0Xa2U; |
Both of these can be combined for the type unsigned long int
.
1 |
54ul, 45UL, 069uL, 096Ul, 0xFFul, 0Xa2UL; |
Floating Point or Real Constants
Numeric constants that have a decimal point are called floating-point or real constants. They can be written in two ways[2]:
- Fractional Forms
- Exponential or Scientific Forms
Fractional Forms
The rules for creating floating-point constants in fractional forms are mentioned below:
- It must contain at least one digit.
- It must have the decimal point.
- It can be both positive or negative but by default positive is assumed.
- Commas, blanks or any other special symbols are not allowed.
1 |
3.14, 69.69, -549.5678, -0.9999; |
Exponential / Scientific Forms
This form is used in cases where the number is either too small or too large. For example, 0.000000314
can be represented as 3.14e-7
. Here, the part of the number before e
is called the mantissa (3.14
) whereas, the part after e
is called the exponent (-7
).
Some rules for creating floating-point constants in exponential forms are mentioned below:
- The mantissa and the exponent must be separated by
e
orE
symbol. - The mantissa can be both positive and negative but by default positive is assumed.
- The exponent must have at least one digit.
- The exponent can also be both positive and negative but by default positive is assumed.
1 |
5.4e8, -69.69E-6, 0.99E10, -0.23e5; |
Floating-point constants are of type double
by default. We can explicitly specify the type as float
by appending f
or F
at the end of the constant. Similarly, we can specify the type as long double
by appending l
or L
at the end of the floating-point constant.
1 2 |
3.14f, -69.69F; 0.00009l, 123.45678L; |
Character Constants
Character constants are single alphabets, digits or special symbols enclosed inside single quotes (' '
).
1 |
'a', 'A', 'c', '1', '$', '^'; |
1 |
"a", "A", "c", "1", "$", "^"; //Wrong...use single quotes only |
Also, the length of a character constant is 1 byte (or 1 character). This means that we can not put more than one character inside the single quote as mentioned below:
1 |
'aA', 'BC', '123', '$@'; //Wrong...only one character is allowed per constant |
As discussed above, everything in computers is stored in binary form. It is easy to understand how an integer or a floating-point constant is stored as binary. But the same is not true for character constants in C programming. This is possible because every character constant in C has a unique integer value associated with it. This is known as the ASCII value. The table below shows characters in the English language in their ASCII representation:
Backslash Character Constants
As discussed in the Keywords & Identifiers lesson, C supports some character constants having a backslash in front of it. These backslash characters have a specific meaning which is known to the compiler. They are also termed as “Escape Sequences”.
Escape Sequence | Name | Use |
---|---|---|
\n | Newline | Used to move the cursor to the beginning of a new line |
\t | Tab | Used to move the cursor to the next Tab stop |
\b | Backspace | Used to move the cursor back by one space on the current line |
\r | Carriage Return | Used to move the cursor to the beginning of the current line |
\a | Bell(alert) | Used to produce a beep sound from the system |
\\ | Backslash | Used to print the Backslash (\ ) character |
\0 | Null | Denotes a null character |
\' | Single Quote | Used to print the Single Quote (' ) character |
\" | Double Quote | Used to print the Double Quote (" ) character |
String Constants
String constants in C consist of zero or more characters enclosed in double quotes (" "
). At the end of every string, the compiler places the null character ('\0'
).
1 2 3 |
"Hello World"; "1234-5678"; ""; // This is an empty string. It consists of only the null character added by the compiler |
Symbolic Constants
Constants in C that are to be used multiple times in a program can be given a specific name. For example, if we want to use the constant Î = 3.141592
at multiple places in the program then instead of writing the long number everywhere, we can define it as a symbolic constant. They are generally defined at the beginning of a program using the syntax mentioned below:
1 |
#define NAME VALUE |
Here, #define
is a preprocessor directive just like #include
. This is the reason that the statement does not end with the semicolon (;
). NAME
here indicates the name we want to give to our constant. VALUE
is the actual numeric, character or string constant that we want to use multiple times in our program.
1 |
#define PI 3.141592 //Declaring symbolic constant |
When the program is compiled, the preprocessor replaces every occurrence of PI with 3.141592 in the program. The use of symbolic constants makes the program much more flexible. Taking the example of the above case, if we now want to increase the accuracy of PI
from 3.141592
to 3.14159265359
. We can do so easily by just updating the symbolic constant as mentioned below instead of updating every occurrence of the constant in the program.
1 |
#define PI 3.14159265359 //Updating symbolic constant |
How to Declare Constants in C?
Apart from the symbolic constants mentioned above which are declared using the preprocessor directive #define
, all other types of constants are declared using the keyword const
. Some examples are mentioned in the code snippet below.
1 2 3 |
const int a =10; //Numeric Constant const float b = 3.14; //Floating Point Constant const char ch = 'A'; //Character Constant |
References
- C Programming 101 – Constants in C(OverIQ)
- C-Constants-Real Numbers(Tutorials Point)
- C Tutorials – Constants in C(W3schools)
Best C Programming Books
List of C Programming books curated for beginners as well as experienced programmers.
Sharing is Caring
If you liked this post, then feel free to share it with your loved ones!