I have a simple question about static variables. If I declared a static variable in a function:
void main() { int k = 0 while(k<=4) { fun(); k++; } } int fun() { static int i=5; i++; printf(Value %d\t", i); return 0; } As I know, the function will deallocate after returning. But where is the i value stored. Is a static variable like a global variable.
Is static is like global variable.No!!!! it's same in terms of life time.different in terms of visibility.void main()which is non-standard and only valid on Microsoft Windows with a Microsoft C compiler, and you haveint fun()and don't use its return value. It would be more orthodox to haveint main(void)andvoid fun(void)— though you'd have to define or declarefun()beforemain()if you changed its signature (though C89 compilers such as MSVC don't mind about the implicitintrule for functions, but that's a 25-year old standard and the 15-year old C99 standard outlawed implicit declarations for functions).