Constant Correctness In C - Online Article

Constant correctness means the right use of const keyword in 'c' code. If we properly use this keyword it will give right result, otherwise it cause errors. It will not affect the runtime. It will cause only Compile time error.

It is necessary to give a initial value to the constant. Constant are the data object whose value will not change during the life time of the program. It is good to use constant correctly from the declaration of const.

Note:- All codes are compiled in Borland Terbo C++ (3.0).

The const Keyword

Const is a keyword in 'C' language. Which is use to define a variable as a constant It is a modifier.


const Datatype variablename = value;


const float pi = 3.14;

but if we want to modify it means we try to assign another value to it ,it will give an error.

const int i = 9;
i = 2; //Incorrect

It will give an error that "Cannot modify a constant variable".

Since we are talking about const lets look at the following declarations.

int const *p;
const int *p;

Is the declaration are different from each other Read such declarations from right-left.

int const *p; //p is a pointer to a constant integer
const int *p; //p is a pointer to an integer that is constant

It means that you can modify what the pointer p is pointing to, but you can not change the contents of the pointer (*p).

int x = 9;
int y = 8;
int const *p = &x; //We can use like this 
p = &y; //We can change what the pointer points to 
*p = 7; //Error accurse we can not change the contents of what it points to

To have a pointer that can change the contents of what it points to, but not what it is pointing to, we do something like this.

int * const p; //p is a constant pointer to an integer

It means that p is a pointer that only points to any integer type variable and we can change the value of that variable.

Increment / decrement Operator in 'C' (++/--)

These operators are use to increment or decrement the value of variables.


int i=9; //initialize i as a integer type
i++; //increment the value of i. 

It will increase the value of i by 1, now value of i will be 10.

If we use increment/decrement operator with character type variable it will increase or decrease the value of character variable. For increment it uses the ASCII value of the character and increment it by 1.


char ch='m'; //initialize character type variable ch
ch++; //increment in value of ch 

now, the value of ch will be 'n'.

The ASCII value of m is 109 ,it will increase it by 1 & new value will be 110 which is the value of 'n' ,hence it will be 'n'. Increment\decrement operator can also be use with pointers. It increases or decreases the value of pointer according to the data type whose variables address pointer hold. If the pointer holds the address of a integer type variable the it will increase address by 2 bytes. If the pointer holds the address of a character type variable the it will increase address by 1 byte.

1000 1001 1002 1003


1000 1002 1004 1006
5 10 15 20






Here the example (a) has an array of characters ,and p is a pointer of character type it have the value of starting address of the array.


If we increment pointer ,it will now points to b means the value of pointer will be 1001. On the other hand in example (b) there is an array of integers and the starting address of the array is 1000. q is a pointer of integer type which contains to the starting address of the array means 1000.


If we increment the q then the value of pointer will be 1002, means it will increase by 2 byte, which is the size integer take to store in memory.


There are two type of increment/decrement and decrement operator:

  1. Post increment/decrement operator
  2. Pre increment/decrement operator

Post increment operator first use the value of variable then increment it. Means firstly it will completely execute the statement than increase the value. But the pre increment operator first increments the value of variable then use it.

Take these examples:

Example 1

	int i;
	i=i++ + ++i;

Answer: 13.

Here program will take i, then post increment operator is in countered hence it will store in stack that it have to increase the value of i, after the addition operator again pre increment operator is in countered hence it will increase the value of i now the value of i is 6, now the value of expression will be calculated it have 2 i to add the first i also have the value 6 because the memory allocation is same so the sum will be 12. But it has a remaining command to increment the value of I hence it will increment it and the value of I will be 13.

Example 2

	int i;

Answer: 5 6

Because of post increment operator first it print the value of i then increment it so in the second printf the value of i is 6.

Example 3

	int i=5;

Answer: It will give an error that "Lvalue required".

First increment operator is encountered it will increase the value of i now it is 6 and we again want to increment it, but now it is 6 which is a constant so we can not increment the value it, hence it will give error.

Example 4

	int d;

Answer: 19

It will start then ++ operator will be encountered (Post increment) after this addition operator will be encountered and it will add 9 and 9 the sum will be 18 but it have an increment operator to proceed hence the value of d will be 19.

Example 5

	int d;

Answer: The answer will be an error "Lvalue Required"

When it will start ++ operator will be encountered , but because of not any space between operators it will not take single + it will take ++ operator so it will cause an error that Lvalue Required.

About the Author:

No further information.


No comment yet. Be the first to post a comment.