Need globals without name collisions? Static at file level!
In C programming, the static
keyword can be used in different contexts. When static
is used at the file level, also known as file scope or global scope, it gives the variable or function "internal linkage." This means that the variable or function is only visible within the file it is declared in, even though it retains its value or state across different function calls or throughout the program's execution.
Let's break this down with an example:
Example Code
// File: main.c
#include <stdio.h>
static int globalCounter = 0; // static variable at file scope
static void incrementCounter() { // static function
globalCounter++;
}
int main() {
printf("Counter: %d\n", globalCounter); // prints 0
incrementCounter();
printf("Counter: %d\n", globalCounter); // prints 1
// The function and the variable are accessible here
return 0;
}
// Another file, for instance, anotherFile.c, cannot access globalCounter or incrementCounter.
Explanation
Static Variable at File Scope (
static int globalCounter = 0;
):globalCounter
is a global variable, but because it's declared asstatic
, it can only be accessed within the filemain.c
.It retains its value between different function calls within the same file.
It cannot be accessed or modified by other files, even if they are part of the same program.
Static Function (
static void incrementCounter()
):This function is only visible within
main.c
. It cannot be called from other files.Its purpose in this example is to increment the
globalCounter
.
Access and Scope:
Within
main.c
, bothglobalCounter
andincrementCounter
can be used freely.If you try to use them in another file, such as
anotherFile.c
, the compiler will throw an error because these are not visible outsidemain.c
.
Benefits:
This use of
static
helps in encapsulating the code. It restricts the scope of variables and functions, reducing potential conflicts, especially in large programs with multiple files.It also aids in maintaining the state of variables across function calls without making them globally accessible.
This kind of scoping is particularly useful in larger projects where you want to avoid global namespace pollution and maintain a clean separation of components.
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.