Let only one global live! The extern keyword
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.
At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.
In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.
To demonstrate the use of the extern keyword with global variables in C, I'll create a simple example involving two source files. The extern keyword is used to declare a global variable in one file, which is defined in another. This approach allows multiple files to access the same global variable.
File 1: main.c
#include <stdio.h>
extern int globalVar; // Declaration of the global variable
int main() {
printf("Value of globalVar in main.c: %d\n", globalVar);
return 0;
}
File 2: globals.c
int globalVar = 10; // Definition of the global variable
Compilation and Execution Commands
gcc -c globals.c # Compiles globals.c into globals.o
gcc -c main.c # Compiles main.c into main.o
gcc -o program main.o globals.o # Links both object files into one executable
./program # Executes the compiled program
Explanation
globals.c: This file contains the definition of
globalVar. This means that the actual storage for the variable is allocated here.main.c: Here,
extern int globalVar;is a declaration, not a definition. It tells the compiler thatglobalVarexists somewhere in the program, but its memory is allocated elsewhere (in this case, inglobals.c). This file usesglobalVaras if it were declared locally.Compilation and Linking:
gcc -cis used to compile the.cfiles into object files (.o). This does not create an executable but prepares the code for linking.gcc -o programlinks the two object files into a single executable namedprogram. This step resolves the reference toglobalVarinmain.cby linking it to the definition inglobals.o.
One Copy in Memory: Despite being accessed from two different files, there is only one copy of
globalVarin memory. Theexternkeyword enables this single-instance access without duplicating the variable.
This approach is crucial in larger projects, where variables need to be shared across different files without duplicating their definitions, thereby maintaining consistency and reducing memory usage.