Scope of variables in C
By Flavio Copes
Learn how variable scope works in C: local variables live inside a function on the stack and vanish when it ends, while globals stay available everywhere.
When you define a variable in a C program, depending on where you declare it, it will have a different scope.
This means that it will be available in some places, but not in others.
The position determines 2 types of variables:
- global variables
- local variables
This is the difference: a variable declared inside a function is a local variable, like this:
int main(void) {
int age = 37;
}
Local variables are only accessible from within the function, and when the function ends they stop their existence. They are cleared from the memory (with some exceptions).
A variable defined outside of a function is a global variable, like in this example:
int age = 37;
int main(void) {
/* ... */
}
Global variables are accessible from any function of the program, and they are available for the whole execution of the program, until it ends.
Blocks have their own scope
Scope in C is not just about functions. Any block delimited by curly braces creates a scope.
A variable declared inside an if or a for only exists inside that block:
int main(void) {
for (int i = 0; i < 3; i++) {
int doubled = i * 2;
}
/* i and doubled don't exist here */
}
Try to use doubled after the loop and the compiler stops you with an “undeclared identifier” error. That’s scope doing its job.
What happens when names collide
A local variable can have the same name as a global one. Inside the function, the local wins. We say it shadows the global:
#include <stdio.h>
int age = 37;
int main(void) {
int age = 8;
printf("%d\n", age); /* 8 */
}
The global age still exists. You just can’t reach it from main() while the local one is in scope.
Local variables that survive the function
I mentioned that local variables are not available any more after the function ends.
The reason is that local variables are declared on the stack, by default, unless you explicitly allocate them on the heap using pointers, but then you have to manage the memory yourself.
There’s one exception worth knowing: static local variables. They keep their value between calls, while staying visible only inside the function:
#include <stdio.h>
void increment(void) {
static int count = 0;
count++;
printf("%d\n", count);
}
int main(void) {
increment(); /* 1 */
increment(); /* 2 */
}
Without static, both calls would print 1.
A classic mistake
Since local variables live on the stack, never return a pointer to one:
int *getAge(void) {
int age = 37;
return &age; /* wrong */
}
The variable is gone when the function returns, so that pointer points to memory the program will reuse. Reading it is undefined behavior. Sometimes it appears to work, then breaks later, which makes it painful to debug.
The fix: return the value itself, make the variable static, or allocate it on the heap with malloc() and free it when done.
Want me to talk about your product? You can sponsor this site.