C Global Variables

By

Learn how global variables work in C, defined outside any function so any function can read and update them, and how they differ from local variables.

~~~

A global variable in C is a variable defined outside of any function. Any function in the program can read it, and any function can update it. A local variable, on the other hand, only exists inside the function that declares it.

In the C variables and types post I introduced how to work with variables. In this post I want to go deeper on the difference between global and local variables.

Local variables

A local variable is defined inside a function, and it’s only available inside that function.

Like this:

#include <stdio.h>

int main(void) {
  int age = 0;
  age += 10;
  printf("%d", age); //10
}

age is not available anywhere outside the main function.

The memory for a local variable is allocated when the function is called, and freed when the function returns. Call the function again, and you get a fresh variable.

Global variables

A global variable is defined outside of any function, like this:

#include <stdio.h>

int age = 0;

int main(void) {
  age += 10;
  printf("%d", age); //10
}

A global variable can be accessed by any function in the program. Access is not limited to reading the value: the variable can be updated by any function.

Due to this, global variables are one way we have of sharing the same data between functions.

Here increment() updates count, and main() sees the new value:

#include <stdio.h>

int count = 0;

void increment(void) {
  count++;
}

int main(void) {
  increment();
  increment();
  printf("%d", count); //2
}

How long do they live?

The main difference with local variables is the lifetime. The memory allocated for a local variable is freed once the function ends.

Global variables are only freed when the program ends. They exist for the entire run of the program.

There’s another nice property: global variables are initialized to zero automatically if you don’t give them a value. Local variables are not. An uninitialized local variable contains whatever garbage was in that memory location.

Be careful with globals

Sharing data this way comes at a cost. When any function can change a global variable, it gets hard to tell where a value came from. Bugs become harder to track down as the program grows.

One concrete pitfall is shadowing. If you declare a local variable with the same name as a global one, the local wins inside that function:

#include <stdio.h>

int count = 10;

int main(void) {
  int count = 0;
  printf("%d", count); //0, not 10
}

You think you’re updating the global, but you’re working on a different variable. The fix is to pick a different name for the local variable, or better, pass the value to the function as a parameter instead of relying on the global.

My advice is to prefer local variables and function parameters, and reach for globals only when several functions genuinely need to share the same state.

Tagged: C · All topics
~~~

Related posts about clang: