// playExternCaller.c // // // Created by Ying Li on 2/23/20. // // This is the main program // // Compile: // gcc -o playExternCaller playExternCaller.c playExtern.c // // Illustrates that the proper way to make a truly global variable // is to use the extern keyword. // declare it with the extern keywork in the header file of the file that gets to // claim some sort of ownership over the variable. // then define it (i.e. give it a value) in the .c file // then other files that want to use it include the .h file and can use it, with ] // all files referring to the same variable in the same location of memory. #include "playExtern.h" //extern int glbA; // can include but don't need because we include this // when we #include playExtern.h // int glbA; // not appropriate way // We do not need to declare glbA because we included playExtern.h int main () { printf("main: Caller gblA %d at Address %p\n", gblA, &gblA); printExternVal(); printf("main: Caller gblA %d at Address %p\n", gblA, &gblA); gblA = 10; printf("main: Caller gblA %d at Address %p\n", gblA, &gblA); printExternVal(); return 0; }