Let only one global live! The extern keyword

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 that globalVar exists somewhere in the program, but its memory is allocated elsewhere (in this case, in globals.c). This file uses globalVar as if it were declared locally.

  • Compilation and Linking:

    • gcc -c is used to compile the .c files into object files (.o). This does not create an executable but prepares the code for linking.

    • gcc -o program links the two object files into a single executable named program. This step resolves the reference to globalVar in main.c by linking it to the definition in globals.o.

  • One Copy in Memory: Despite being accessed from two different files, there is only one copy of globalVar in memory. The extern keyword 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.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

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.