Identifiers have three types:
Let’s discuss about Constant. In general, anything that is not changed is constant. But in C language, a software always manages data or information. This information is called Constant.
Data = Information = Constant
Const is a keyword in C language. It is identified as a Qualifier. A Qualifier Const can be applied to the declaration of any variable to specify that its value will not be changed.
Classification of Constant
There are two types of Constant present in the C language. They are:
- Primary Constant
- Secondary Constant
1. Primary Constant
Primary Constant has three types:
Example: -55 , 26, 0 etc.
Example: 19.6, -1.65, 3.1 etc.
Example: ‘ c ’, ‘ j ’, ‘ + ’, ‘ 3 ’ etc.
2. Secondary Constant
They are various types of Secondary Constant:
Const was not applicable in the early use of the C language; the concept was borrowed from C++.
The keyword Const can be applied to any declaration including structure, unions, and enumerated types or typedef names. The process of applying the keyword Const to a declaration is called “qualifying the declaration”. Const means something is not modifiable.
Programming Example 1:
int main ()
const int x= 5;
printf("x = %d",x);
return 0 ;
The program produced a compiler error because we tried to increment the value of x. X is constant and we cannot change the values of a constant.
Programming Example 2:
int main ()
const int x;
printf("x= %d ",x);
return 0 ;
In this example, we declared a constant variable “x” without initialization. If we do not initialize at the time of declaration for a constant, the garbage value will be assigned to x and it will not be initialized. For this const variable, we have to initialize at the time of declaration. In this particular program, the garbage value of constant x is zero.
Programming Example 3:
In this program, we tried to change the value of a constant with the help of a pointer. Pointer refers to the address of a constant.
We can modify the value of any const variable when we declare a pointer “int *p”. P is a pointer which points to the int type of a variable.
If we modify through the variable “x” by declaring a pointer “++( *p )-> *p”, the result is a block. But if we modify through the variable “p”, an error will occur.
Constant values remain unchanged throughout the program, but we can modify the value of a constant through a pointer.
Programming Example 4:
In this programming example, we used another Const pointer. By using a Const pointer, we cannot change the value of a pointer. It means that we can’t declare a “p++” pointer since it is not a Const and the variable it points is constant. Therefore, a pointer “++(*p)” is not applicable. To declare a Const pointer to Const, the pointer must be a Const and the variable it points is also a Const.
How to declare a pointer:
int const *p;
Both are the pointers to constant.
How to declare a const pointer:
Programming Example 5:
In this programming example, the value of x is a Const. The pointer itself is a Const. Changing the value of a pointer is not possible; an error will occur.
The declared variable is “int *const p= &x” where “p” is initialized at the time of declaration.
The pointer itself is a Const but the pointer to Const is not applicable.
Programming Example 6:
This particular program cannot be compiled. Since it is a pointer Const, we cannot change the value of “p”.
Programming Example 7:
A compile time error occurred in this particular program because we cannot change the value of “p” since it is a Const pointer to Const.
Constant is a powerful basic concept of the C language. There are many types of constant present in the C language. Without the concept of constant, data or information cannot be managed properly by the software in the C language. Related articles are available on the website.