I felt I was a bit grey in this aspect and I found this : http://www.phim.unibe.ch/comp_doc/c_...age_class.html.
He says,
"static' can also be defined within a function. If this is done, the variable is initalised at compilation time and retains its value between calls. Because it is initialsed at compilation time, the initalistation value must be a constant. "
I just tried out a simple code where I didnt initialise the static variable. I was expecting some warning to be displayed when I compiled it with the -Wall option. Instead the static variable was initialised to 0.
Can anyone shed some light on this.
Here's the code :
And the output :Code:# include<stdio.h> int func() { static int i; i++; return (i); } int main() { int count=0; while(count < 5) { printf(" count :%d, func = %u\n",count,func()); count++; } return 0; }
Code:count :0, func = 1 count :1, func = 2 count :2, func = 3 count :3, func = 4 count :4, func = 5



LinkBack URL
About LinkBacks


