Scope – C

Every variable in C has its lifetime meaning when and for how long will it exist during the execution of the program. Usually the scope of a variable can be determined by the curly braces. Variables can be used as long as they are within the scope.

In C language there are different type of variables which have different lifetime:

  • Global variables – the whole program
    • Can be used everywhere in the code
  • Static variables – the whole program
    • Can only be used in the scope where it was declared
  • Local variables – during the scope of the variable
    • Can only be used in the scope where it was declared

See example at the end to understand how scope works.

Global variables

Global variables exist throughout the whole program, they should have an unique name so that it would not conflict with local variables (it will create a compile error). Using these in command line programs is a bad practice. To avoid using global variables one should try to pass data to functions by reference.

Even though it is a bad practice to use global variables in command line programs, it is widely used in embedded systems. This is because the program is run infinitely and program flow will depend on interrupts and events and all of the functions should be able to modify the necessary data.

Static variables

Static variables can be created by using the static keyword. This will create a variable which will remain in the memory until the end of program execution. The difference between global variable is that these variables can only be used within the scope where the variable was declared.

The usage of these variables would be when keeping the value of some data in the previous function call is desired. Meaning that on each function call the static variable will keep its value whereas local variables would be created again and lose its value.

Local variables

Local variables are just regular variables which lifetime is within its scope (the surrounding curly braces). When dealing with functions, on each call to the function, these variables are created again. Whenever a function has arguments by value, those are local variables as well which as a life cycle of the execution of the function.

Example

In this example there are 7 variables, each of them are included in the printf calls. The program will print out either the value, ? when variable does not exist in that scope or   when variable exists however it is not possible to access it.

Print functions are called:

  • at the beginning of the program (beg)
  • inside the custom scope (m: )
  • after the custom scope in a loop (l: )
  • before exiting (end)
#include <stdio.h>

// Global variable
int global = 25;

void number(int arg);

int main(void) {
    // Local variable
    int local = 0;
    printf("   |%6s|%6s|%6s|%6s|%6s|%6s|%6s|\n",
        "global", "local", "stat",
        "arg", "func", "i", "scop");
    
    printf("beg|%6d|%6d|%6s|%6s|%6s|%6s|%6s|\n", 
        global, local, "-", "?", "?", "?", "?");
    printf("_____________________________________________________\n");
    for (int i = 0; i < 5; i++) {
        { // Scope - DO NOT DO THIS!
            int scop = 66;
            // Increment values
            global++;
            local++;
            scop++;
            // Call function
            number(local);
            
            printf("m: |%6d|%6d|%6s|%6s|%6s|%6d|%6d|\n", 
                global, local, "-", "?", "?", i, scop);
        }
        printf("l: |%6d|%6d|%6s|%6s|%6s|%6d|%6s|\n", 
            global, local, "-", "?", "?", i, "?");
        printf("_____________________________________________________\n");
    }

    printf("end|%6d|%6d|%6s|%6s|%6s|%6s|%6s|\n", 
        global, local, "-", "?", "?", "?", "?");

    
    return 0;
}
    
void number(int arg) {
    static int stat = 0;
    int func = 0;
    // Increment variables
    arg++;
    func++;
    stat++;
    printf("f: |%6d|%6s|%6d|%6d|%6d|%6s|%6s|\n", 
        global, "-", stat, arg, func, "-", "-");
}

This will output:

   |global| local|  stat|   arg|  func|     i|  scop|
beg|    25|     0|     -|     ?|     ?|     ?|     ?|
_____________________________________________________
f: |    26|     -|     1|     2|     1|     -|     -|
m: |    26|     1|     -|     ?|     ?|     0|    67|
l: |    26|     1|     -|     ?|     ?|     0|     ?|
_____________________________________________________
f: |    27|     -|     2|     3|     1|     -|     -|
m: |    27|     2|     -|     ?|     ?|     1|    67|
l: |    27|     2|     -|     ?|     ?|     1|     ?|
_____________________________________________________
f: |    28|     -|     3|     4|     1|     -|     -|
m: |    28|     3|     -|     ?|     ?|     2|    67|
l: |    28|     3|     -|     ?|     ?|     2|     ?|
_____________________________________________________
f: |    29|     -|     4|     5|     1|     -|     -|
m: |    29|     4|     -|     ?|     ?|     3|    67|
l: |    29|     4|     -|     ?|     ?|     3|     ?|
_____________________________________________________
f: |    30|     -|     5|     6|     1|     -|     -|
m: |    30|     5|     -|     ?|     ?|     4|    67|
l: |    30|     5|     -|     ?|     ?|     4|     ?|
_____________________________________________________
end|    30|     5|     -|     ?|     ?|     ?|     ?|

Resources

  1. Lifetime [ Link ]
  2. Scope Rules [ Link ]