Global Variables in C

A global variable in C language is a variable declared outside of any function. A global variable has static lifetime and global visibility at file level.

This lesson explains how to declare a global variable in C language and what are its advantages and disadvantages.

Global Variables

We have seen that to pass information to a function it is possible to use parameters.

However, the use of parameters is not the only way. In fact, it is also possible to use Global Variables, in English Global Variables. Often these variables are also called external variables, in English External Variables.

By definition, a global variable is a variable declared outside of any function. A global variable has the following properties:

  • Static lifetime: the global variable exists for the entire duration of the program execution. In other words, the global variable exists as long as the program is running. In this, a global variable is very similar to a static local variable.
  • Global visibility at file level: a global variable is visible throughout the file in which it is declared. In other words, a global variable is visible throughout the file in which it is declared, including the functions declared in the file itself. Consequently a global variable can be accessed and potentially modified by any function in the file.
Definition

Global Variables

In C language a global variable is a variable declared outside of any function.

A global variable exists for the entire duration of a program execution and can be accessed and potentially modified by any function in the file in which it is declared.

A very important detail concerns the location of global variables.

We have seen, previously, that to a running process corresponding to a C program at least four memory areas or segments are allocated.

Of these segments we have studied the purpose of only one, the text segment. This segment contains the machine code of the program.

Now we can understand the purpose of a second segment, the data segment. This segment contains the global variables and the static variables that we saw in the previous lesson.

Data memory segment in which global and static variables are stored
Picture 1: Data memory segment in which global and static variables are stored

Declaration of a Global Variable

To declare a global variable, it is sufficient to declare it outside of any function. For example, suppose we want to implement a pair of functions that realize a counter that can grow or decrease. The first function increment increments the counter by 1, while the second function decrement decrements the counter by 1. The counter is a global variable that is declared outside of any function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>

/* Declaration of a global variable */
int counter = 0;

void increment() {
    /* Increments the counter by 1 */
    counter++;
}

void decrement() {
    /* Decrements the counter by 1 */
    counter--;
}

int main() {
    int proceed = 1;

    while (proceed) {
        int choice;

        printf("Counter: %d\n", counter);
        printf("1. Increment\n");
        printf("2. Decrement\n");
        printf("3. Quit\n");
        printf("Choose: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                increment();
                break;
            case 2:
                decrement();
                break;
            case 3:
                proceed = 0;
                break;
            default:
                printf("Invalid choice\n");
        }
    }

    return 0;
}

The program is very simple. The counter is a global variable that is declared outside of any function. The functions increment and decrement increment and decrement the counter by 1. The main function prints the value of the counter and asks the user to choose whether to increment or decrement the counter. If the user chooses to exit, the program terminates.

Trying to compile and execute the program, we can obtain a result very similar to the following:

Counter: 0
1. Increment
2. Decrement
3. Quit
Choose: 1
Counter: 1
1. Increment
2. Decrement
3. Quit
Choose: 1
Counter: 2
1. Increment
2. Decrement
3. Quit
Choose: 2
Counter: 1
1. Increment
2. Decrement
3. Quit
Choose: 2
Counter: 0
1. Increment
2. Decrement
3. Quit
Choose: 3

Disadvantages of Global Variables

Global variables are very useful for the realization of programs. However, like all things, global variables also have pros and cons.

Using global variables can be very convenient when many functions need to share data or when few functions share large quantities of variables. However, in many cases it is better to exchange data between functions using parameters rather than sharing variables. The main reasons are:

  • Since a global variable is potentially modifiable from any point in our source file, it is very difficult to understand where and when a global variable is modified. Therefore, often debugging a program that uses global variables can be very difficult. Moreover, a function that modifies a global variable can have undesired effects on other functions that use the same global variable.
  • If we modify the name or type of a global variable, for example when maintaining a program, we must then remember to also modify all the functions that use the variable itself. This can be very complicated if the program is very large.
  • If an incorrect value is assigned to a global variable it is difficult to identify the function or code point that caused the problem.
  • Functions that use global variables are difficult to reuse in other programs. In other words they are not very portable.

In short, the use of global variables often leads to more complex and difficult to maintain programs.

In Summary

In this lesson we have studied global variables. In particular we have seen that:

  • A global variable is a variable declared outside of any function.
  • A global variable has the following properties:
  • Static lifetime: the global variable exists for the entire duration of the program execution.
  • Global visibility at file level: a global variable is visible throughout the file in which it is declared.
  • Global variables are very useful for the realization of programs. However, like all things, global variables also have pros and cons.

In the next lesson we will study the block construct in C language.