We prefer const qualifier when we don’t want to change the value of any object in our program. In this article, I will discuss the const qualifier and try to clear the concept of const and in the last of this article, I will discuss some important concept related to const, which is frequently asked in an interview.

const is only a qualifier, it changes the behavior of variable and makes it read-only type.When we want to make an object read-only type, then we have to declare it as const.

Syntax

const DataType Identifier = Value;
e.g.
const int iData = 0;

At the time of declaration const qualifier only give the direction to the compiler that value of declaring object cannot be change. In simple word,const means not modifiable (cannot assign any value to the object at the run time).

const

When we declare an identifier as constant then it depends on the implementation (depend on upon c compiler), where the constant store in process control block (PCB) of a program. In many machines constant and jump table store in text section (it’s read only and contain all executable instruction).

Note: We can change the value of constant with the help of pointer. Constant are fast compare to variable and processor generates more optimize code with the constant.

There are few rules, which help to decide when beneficial to use the const keyword in the program.

  • When you have no need to change the value of the variable after initialization.
  • In call by reference function argument, if you do not want to change the actual value which has passed in function (e.g., int Display ( const char *pcMessage) ) .
  • In the case of I/O memory mapped register , fields of structure and union should be constant.
  • Use type safe constant in place of micro as for as possible (e.g., static int const icData = 1234 ) .

following the way to use const keyword with pointers

Below, I am describing some important example which  generally uses in the program.

  • Pointer to a constant object

const int *piData = 0;
or
int const *piData = 0;

Here piData is the simple pointer which points to const int, it means *piData is a const-qualified type and if you want to change the value of *piData then you will get the compiler error.
In above example, piData is modifiable it can be the point to other objects but the value of the pointed object will never change.

OutPut:

In function ‘main’:error: assignment of read-only location ‘*piIndex’
*piIndex = 3;

  • Constant pointer to a non-constant object

int *const piData = 0;

piData is itself not modifiable because of its qualified by the const but as per the above declaration, It points to an object which is modifiable, means we can change the value of the object which pointed by piData.

OutPut:

*piIndex is 2
*piIndex is 4

  • Constant pointer to a constant object

const int *const piData=0;
In above declaration piData and *piData both qualified by const type. So in this scenario, you cannot change the value of piData or *piData,if you will try to change the value of piData or *piData then you will get the compiler error.

Everything will be good if you do not try to change the value of piData and *piData 

OutPut:

*piIndex is 2

If you will try to change the value of *piData then you will get the compiler error because here *piData qualify to constant.

OutPut:

assignment of read-only location ‘*piIndex’
*piIndex = 3; //change the value

Here pointer is also constant so if you want to change the address that pointed by pointer then, you will get the compiler error.

OutPut:

In function ‘main’:
error: assignment of read-only variable ‘piIndex’
piIndex = &iIndexData2; /*It’s not valid because pointer is constant here*/