c - Redefined Global variable -
i'm little confused code result:
#include <stdio.h> int g; void afunc(int x) { g = x; /* sets global whatever x */ } int main(void) { int g = 10; /* local g 10 */ afunc(20); /* function set 20 */ printf("%d\n", g); /* print "20" */ return 0; }
why result 10 not 20 ?
calling afunc
changes global g
, , main
retains local g
.
entering function doesn’t swap scope global scope. each function* has own scope.
* among other things
Comments
Post a Comment