Variables Visibility or Scope in C

The Scope of a variable indicates the context in which the variable itself is visible. In this lesson we will see how the C compiler manages the visibility of variables.

In particular we will see the general rule of variable visibility.

Visibility rule

In the previous lessons we studied local variables, global variables and code blocks. In each case we studied what the visibility rules of a variable are depending on the cases.

Now let's try to summarize in a general rule how the compiler manages the visibility of variables.

Definition

Variable visibility rule

When the C compiler encounters a variable, it tries to resolve its name, that is to find its declaration. The search for the declaration occurs based on the following rules:

  1. If the name of the variable is the same as the name of a local variable, the compiler uses the declaration of the local variable.
  2. In case there is no local variable with the same name, the compiler searches for a local variable with the same name within the outermost code block.
  3. If there is no local variable with the same name within the outermost code block, the compiler searches for a global variable with the same name.

Keeping in mind this general rule we can understand what the behavior of the C compiler will be when it encounters a variable.

To better clarify everything let's study an example.

Summary example

Let's consider the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/* Declaration 1 */
int i = 1;

void f(int i) {     /* Declaration 2 */
    i = 2;
}

void g() {
    int i = 3;      /* Declaration 3 */

    if (i > 0) {
        int i = 4;  /* Declaration 4 */
    }

    i = 5;
}

void h() {
    i = 6;          /* Declaration 5 */
}

The above example, although a bit extreme, allows us to understand in depth the variable visibility rule.

Let's go in order:

  1. The variable i declared at line 2 is a global variable.
  2. The variable i declared inside the function f at line 4 is a parameter and therefore is a local variable. Having the same name as the global variable, it will shadow the global variable inside the body of function f.
  3. The variable i declared inside the function g at line 9 is a local variable. Inside the body of g it will shadow the global variable i.
  4. The variable i declared inside the if block at line 12 is a local variable of the if block. Inside the body of the if block it will shadow the local variable i declared inside the function g.
  5. The variable i used at line 15 is traceable to the variable i declared inside the function g at line 9.
  6. The variable i used at line 19 is traceable to the global variable i declared at line 2 since there is no local variable with the same name.

In Summary

In summary, the variable visibility rule tells us that:

  1. If the name of a variable is the same as the name of a local variable, the compiler uses the declaration of the local variable.
  2. If the name of a variable is not the same as the name of a local variable, the compiler searches for a local variable with the same name within the outermost code block.
  3. If there is no local variable with the same name within the outermost code block, the compiler searches for a global variable with the same name.

At this point we have clear the concepts of visibility or scope of variables and their lifetime. In the next lesson we will analyze an important concept that links scope to the execution of functions: Pure Functions and Impure Functions.