DO NOT change your code, but adjusted your comments to indicate what the code is doing. By the way, commenting on your code is a really good idea and makes for a better lab analysis! signed, former graduate student of TA
#include <stdio.h> int g; /* define a global variable void afunc(int x) { g = x; /* this sets the global to whatever x is */ } int main(void) { int g = 10; /* Define and set a Local g to 10 */ afunc(20); /* This function sets global x to 20 */ printf("%d\n", g); /* this prints local g "10" */ return 0; }
Think of this βsearchβ from the main to global storage. You see local g in front of global g, so local g is used.
source share