c - static variables and local -
the output : 100 -10 0 100 -5 2 10. why? after running first time b() static x @ end of b() -5 (i check) why c() gave 0, isn't use static x?
#include <stdio.h> extern int x; void a() { int x=100; printf("% d ",x); x+=5; } void b() { static int x=-10; printf("%d ",x); x+=5; } void c() { printf("%d ",x); x+=2; } int main() { int x=10; a(); b(); c(); a(); b(); c(); printf("%d ",x); return 0; } int x=0;
in
void c() { printf("%d ",x); x+=2; }
it not use static copy of x allocated in defination function b()
.
it use global copy of variable x have declared in last line of program.
int x=0;
change last line of x different value , output changed c()
why global 1 , not static?
so here in scope of static variable x limited body of b()
in c() can not used.
c() depending on global copy of x. if remove global definition of x in c() give compilation error.
Comments
Post a Comment