Monday, June 18, 2012

Const keyword

"const" keyword stands for constants (once declared, not modifiable). In programming language, we use variables to have identification for our real values. So variables are nothing but represents a memory location where our data gets stored. Whenever we want to access data, we can directly retrieve it by referring our variable. In the same way, we can change the data.
For Example:
int data = 100; // data is the variable name, 100 is the value.
data  = data + 10; //modifying the variable's value.
int t = data; //getting value to another variable through old variable.

Now consider a case (mathematical) where we need pi's value (3.14) to get our result. We are sure that pi's value will never change then if we use this value to our normal variable then compiler allow us to modify it. Example:
int pi = 3.14;
pi = pi + 10; //not expected as we know that pi's value cannot be 13.14.

So we need a way to tell the compiler that don't consider this identifier modifiable. We can do that with "const" keyword. Compiler knows the meaning of const and then he will not allow you to modify the value at compile time itself.
Example:
const int pi = 3.14;
pi = pi + 10;//Error: assignment of read-only variable 'pi'

It is really good practice to use const in our program when we know that its value will always be constant.
Till now we discussed "const" in terms of variable's value, but what about pointers (of-course pointer is also a variable holding memory location as its value". Now see below line:

const char *char_ptr = "data is constant""; //What you think? What is char_ptr?
*char_ptr = 'D';//Will compiler allow us to modify the string.
char_ptr = "pointer is not constant";//Will compiler allow us to modify the pointer.

Here 'char_ptr' is a pointer variable pointing to a constant data (i.e. "about const"). If we try to modify the data where this pointer points to, we will get compile time error (i.e. *char_ptr = 'D' will give Error: assignment of read-only variable '* char_ptr'). But pointer is not constant here, so it can point to another data (i.e. char_ptr = "pointer is not constant").
What if we declare pointer like below:

char * const char_ptr = "pointer is constant";
*char_ptr = 'P';
char_ptr = "data is not constant"; //Error: assignment of read-only variable 'char_ptr'

This time our pointer variable (char_ptr) is constant, it can't be assigned. So the compiler will give error at line - char_ptr = "data is not constant". But will allow us to modify the contents of the data i.e.- *char_ptr = 'P'.
Now we have clarity about "const" keyword.
---------------------------------------------------------------------
const int data = 100;//variable data is constant.
const char *char_ptr  = "data is constant";//data where the pointer is pointing to is constant.
char * const char_ptr = "point is constant";//pointer variable char_ptr is constant.
const char * const char_ptr  = "data and pointer are constants";//pointer variable char_ptr and the data where this pointer is pointing to are constants.
---------------------------------------------------------------------

const int data = 3.14;//variable data is constant
int const data = 3.14;//variable data is constant
above both the lines are same (i.e. const int == int const), in both the cases data is the constant variable.
---------------------------------------------------------------------

Please note that constant variables can't be assigned, they are always initialized.

1 comment:

  1. Superb Article....good to know about const in a simple way with good examples

    ReplyDelete