Thursday, July 7, 2011

Difference between const and static in C

Difference between const and static variables are often confusing for the beginners. These are actually important features of C language. A variable declared as const can only be initialised once. The program should not alternate the value of a variable declared as const. A variable declared as const should never appear to the left of an assignment operator throughout the program. The reader may refer the C literature for appreciating the use const variable.

Now consider a static variable. Static variables are permanent variables within their on function. Unlike global variables they are not known outside their function or file, but they maintain their values between function calls.

For better undestanding of the concept consider the code given below.


#include<stdio.h>

void display (void)
{

int j=10;
++j;
printf("J=%d \n",j);

}
 
main(void)
{

int i;
for (i=0;i<10;++i)
display();

}

Output:

J=11
J=11
J=11
J=11
J=11
J=11
J=11
J=11
J=11
J=11
 
In the above program consider the variable 'j'. 'j' is declared as a an ordinary local variable. It's life is only though out the function display(). After the execution of the function variable 'j' is automatically destroyed. Thus when the function is called again variable 'j' is again defined and is initialised to value 10. Thus in all cases the the value of j displayed is 11.

Look at the code below

#include<stdio.h>

void display (void)
{

static int j=10;
++j;
printf("J=%d \n",j);

}

main(void)
{

int i;
for (i=0;i<10;++i)
display();

}

Output:

J=11
J=12
J=13
J=14
J=15
J=16
J=17
J=18
J=19
J=20

Here the difference is that the variable 'j' is declared as static. In this case the variable 'j' will not be destroyed when the function execution is over and in addition, it also retains its last value. Thus the declaration and initialisation of 'j' will occur only once when the function is called for the first time. Thus each time the function display() is called value of 'j' is incremented.

16 comments:

Anonymous said...

output will be till j-19

Anil C S said...

No. The value of j varies from 11-20. As the i varies from 0-9, the function display() is called ten times. The value of j displayed during the first call of function was 11.

Anonymous said...

good ;)

Anonymous said...

niceeeeeeeeeeeeeeeeeee :) :)

Anonymous said...

Can you please explain "const" with an example similar to "static"

Anonymous said...

Very helpful!!!!!!!!
can you please explain constant with example???

Anonymous said...

what about const?

matheen smart vip said...

hlpfull

Anonymous said...

cleared.Thanks.

Anonymous said...

Da anilae Image processorinnu enthu villa verum???

Anonymous said...

madarchod, bhosdiwale..const ka example tera baap dega..

Anonymous said...

thanks bro...

Anonymous said...

great example !!!

Anonymous said...

#include

void display (void)
{

const int j=10;
++j;
printf("J=%d \n",j);

}

main(void)
{

int i;
for (i=0;i<10;++i)
display();

}


will show an error :increment of read only variable 'j'

Anonymous said...

Useful

Anonymous said...

woluld you please tell me the output of this program
#include

void display (void)
{

static const int j=10;
++j;
printf("J=%d \n",j);

}

main(void)
{

int i;
for (i=0;i<10;++i)
display();

}

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes