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.
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:
- 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.
- 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.
- 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 | |
The above example, although a bit extreme, allows us to understand in depth the variable visibility rule.
Let's go in order:
- The variable
ideclared at line 2 is a global variable. - The variable
ideclared inside the functionfat 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 functionf. - The variable
ideclared inside the functiongat line 9 is a local variable. Inside the body ofgit will shadow the global variablei. - The variable
ideclared inside theifblock at line 12 is a local variable of theifblock. Inside the body of theifblock it will shadow the local variableideclared inside the functiong. - The variable
iused at line 15 is traceable to the variableideclared inside the functiongat line 9. - The variable
iused at line 19 is traceable to the global variableideclared at line 2 since there is no local variable with the same name.
In Summary
In summary, the variable visibility rule tells us that:
- 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.
- 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.
- 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.