// 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. // # means "pre-processing command" // #include means "copy-paste playExtern.h into this file before compiling it" #include "playExtern.h" // extern int glbA; // redundant if in playExtern.h // int gblA; // not appropriate way // We do not need to declare gblA 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; }