Const Qualifier in C
In general, the const qualifier is used to declare a variable as constant, meaning its value cannot change once the variable has been initialized. However, there are several benefits of using const, such as if we have a constant value of the PI, we wouldn’t like any part of the program to modify that value. Therefore we can declare it as a const using the const qualifier. But it has a little twist as usual because sometimes we are able to change the values of variables that are declared with const qualifiers. However, this is not always possible as it depends on where the const variables are stored in memory.
Syntax of const variable
In General, the const qualifier only gives only an indication to the compiler that the value of qualify object cannot be changed. In other words, we can say that the const means not modifiable (cannot assign any value to the object at the run time).
However, when we declare an identifier as constant using the const qualifier, it depends on the implementation (or on the compiler) where the “constant variable” will go or stored in the process control block. Thus several machines store the constant and jump tables in the text section, which only reads and contains all other executable instructions.
Rules
There are few rules that can help us decide when to use a const in the C program:
- We can use it when we don’t want to change the variable’s value after the initialization.
- In the case of call by reference, when we don’t want to change the value of the passed variable. For e.g.,
- We can also use it while mapping the I/O register with the help of pointers in C.
Now, let us see some examples in which we will use the const qualifier in our program with a pointer:
1. Pointer to constant
Syntax
As you can see in the above declaration that “xData” is pointing to a constant integer variable. Therefore we cannot change the value of the pointed integer variable using the pointer (*xData), but we can change the pointer to point to any other integer variable. So now it is possible that, the pointer can point to any other variable because it is usually stored in the R&W area (or read and write memory). So lets us see an example of how we can implement it with the help of following the given examples.
Example 1
In this example, we will try to change the value of the integer variable using the pointer to constant (piIndex). However, If a user tries to change the value of the variable (iIndexData1) using the *piData, he will get the compiler error.
Program
Output
In function ‘main’:error: assignment of read-only location ‘*piIndex’
*piIndex = 3;
Example 2
Now, we will try to change the pointing variable in this example.
Program
Output
*piIndex is 2 *piIndex is 4
You can see that the above give code works perfectly, and we have successfully changed the pointing variable:
2. Constant pointer to a constant:
Syntax
As you can see in the above declaration, it is described that the constant pointer is pointing to a constant integer variable. This means we cannot change the value pointed by the pointer, and we can also not point the pointer to other integer variables.
Example 1
In this example, we will use the concept of a constant pointer to a constant.
Program
Output
*piData is 2
As you can see that everything is working fine if we don’t try to change the value of piData and *piData.
Example 2
In this example, we will try to change the value of *piData. However, according to the concept, we will get the compiler error because *piData qualify to constant. Now let’s see what happens:
Program
Output
Error: assignment of read-only location '*piData'
As you can see in the Output, we cannot change the value of “*pidata”.
Example 2
Here we will see that can we can point another integer variable to the pointer. Lets us see what happens:
Program
Output
Error: assignment of read-only variable 'piData'
In the Output of both above given programs, you can see that in case of constant pointer to constant, we cannot change the constant value and neither we can point the constant pointer to another variable.