Course Introduction to Programming
Scope of Variables
In this lesson, we will learn what is the scope of variables, addressing the following topics:
- What is the scope of a variable?
- Local Scope
- Global Scope
- Priority
What is the scope of a variable?
The scope of a variable represents the boundaries within which a variable is defined.
This means that a variable may be defined in one section of the code and not be defined in another section.
Local Scope
To facilitate the explanation, analyze the following code:
#include <stdio.h>
int aux(){
int a = 9;
int b = 123;
}
int main(){
int a = 4;
}
Please note that in the code above, the declaration of the variable
When declaring
Likewise, the variable
With this in mind, analyze the code output below:
#include <stdio.h>
int aux(){
int a = 9;
int b = 123;
// prints the values of variables a and b contained in aux function
printf("aux -> %d %d\n", a, b);
}
int main(){
int a = 4;
// prints the value of the variable a contained in the main function
printf("Main -> %d\n", a);
// calls the function aux
aux();
}
/* Output:
Main -> 4
aux -> 9 123
*/
Observations:
If, in the code above, we try to access the variable
within the main function, Code::Blocks will warn us that was not declared. if, else, while structures, among others, also have local scope. That is, a declared variable inside a while loop only exists and can be accessed inside this structure.
With this in mind, we can now understand what the function parameters are.
The parameters that are passed to a function represent local variables that must be initialized when calling this function.
For easier understanding, analyze the code below:
#include <stdio.h>
int sum(int a, int b){
return a+b;
}
int main(){
printf("%d\n", sum(2, 5));
}
/* Output:
7
*/
In the code above, the variables
Therefore, when calling the function, we must include its parameters, because it needs them. If we don't inform them, Code::Blocks will alert us of an error.
Global Scope
Variables of global scope exist and can be accessed in any part of the program.
These variables must be declared outside of any function.
Example:
#include <stdio.h>
int global = 1; // declaration of the global variable with value 1
int aux(){
printf("%d\n", global); // prints the value of the global variable
global = 3; // changes the value of the global variable to 3
}
int main(){
printf("%d\n", global); // prints the value of the global variable
global = 2; // changes the value of the global variable to 2
aux(); // calls the aux function
printf("%d\n", global); // prints the value of the global variable
}
/* Output:
1
2
3
*/
Priority
In C, local variables have higher priority than global ones.
For easier understanding, analyze the code below:
#include <stdio.h>
int var = 7;
int aux(){
int var = 9;
printf("%d\n", var);
}
int main(){
printf("%d\n", var);
aux();
printf("%d\n", var);
}
/* Output:
7
9
7
*/
In the code above, we had the declaration of the variable var both in the global scope and in the local scope of the function aux.
When executing the first printf command of main, the program printed the value
When calling the aux function, a local variable var is declared with a value of
When returning to main, the second printf command is executed and prints the value
Observation: notice that if we had not included the type (int) before the name var in the aux function, we would not be declaring a new variable, but changing the value of the already existing one:
#include <stdio.h>
int var = 7;
int aux(){
var = 9;
printf("%d\n", var);
}
int main(){
printf("%d\n", var);
aux();
printf("%d\n", var);
}
/* Output:
7
9
9
*/
Scope of variables may be a little confusing at first glance, but you will get the hang of it naturally as you solve more exercises.
Until the next lesson!