In my previous article, I have already discussed the const qualifier and its functionality. If you have some confusion regarding the const or you not familiar with the const qualifier then it’s my personal advice to you, first, you have to read about the const keyword before solving the below questions.

In this article, I will only discuss some important questions related to const qualifier which is frequently asked by an interviewer in the interview.
Note: All program are tested on the dev C++.

Question 1:

[Error] assignment of read-only location ‘*ciData’

Explanation

ciData is the pointer to the constant integer, so we cannot change the value of address which holds by ciData.

Question 2:

Error:  Runtime error

Explanation

The “aticle” is a string literal,so when to try to compile the above program compiler does not throw the error but when you try to run the program. it will be the crash.

Question 3:

Output:

icleworld

Explanation

In above example pcName point to the constant character but itself is not constant.so we can increment the position of the pointer.

Question 4:

[Error] assignment of read-only location ‘*pcName’

Explanation

Here pointer point to character constant so we cannot replace the character.

Question 5:

[Error] increment of read-only variable ‘pcName’

Explanation

In above example pointer itself constant so we cannot increment the pointer.

Question 6:

[Error] increment of read-only variable ‘pcName’

Explanation

In above example pointer is constant and it also points a constant address and it.

Question 7:

OutPut:

Value before the change =5
Value after the change = 6

Explanation

The output may be different on another machine because it may do another thing depending on the particular machine, compiler, and state of the program.

Question 8:

Output:

7

Explanation

In above example, the pointer is not constant only pointed data are constant.

Question 9:

Output:

GarbageData

Explanation
In above example, indirection operator and  pre-increment operator have the same priority level and associative is left-right. So *++p behave like *(++p)  that is why now p point to a garbage data location.

Question 10:

[Error] increment of read-only location ‘*p’

Explanation

In above example, indirection operator and pre-increment operator have the same priority level and associative is left-right.Here ++*p behave like ++(*p), so when to compile this code compiler throw a compilation error because of pointer p point to a read-only address.

Question 11:

[Error] assignment of member ‘iAge’ in read-only object

Explanation

In the above example, when we have declared the union variable constant then all member of the union become constant associated to that object.

Question 12:

OutPut:

aticleworld

Explanation

In the above example, passing argument 1 of ‘strcpy’ discards ‘const’ qualifier from pointer target type.

Question 13:

Output:

arr[3] before calling function = 4
arr[3] after calling function = 11

Explanation

In the above example, passing argument 1 of ‘ReplaceValue’ discards ‘const’ qualifier from pointer target type

Your opinion matters

Although here I have tried to put a lot of points about the const qualifier, I would like to know your opinion regarding the const qualifier in C, so please don’t forget to write a comment in the comment box.