Course Introduction to Programming
Scope of Variables
In this lesson, we will learn about one of the essential concepts of programming: Variable Scope.
We will understand what scope is, how it defines where a variable can be used, and explore the different types of scope โ local and global โ through practical examples in C. We will also see how scope influences the behavior of variables, what happens when there are variables with the same name in different scopes, and why global variables are automatically initialized to zero. By the end of the lesson, you will be able to correctly identify the scope of each variable and write more organized, secure, and predictable programs ๐
What is the scope of a variable?
The scope of a variable represents the region of the program where it is recognized and can be used. In other words, scope defines the limits within which a variable "exists".
Thus, a variable may be available in one part of the code but not be visible in another, depending on where it was declared.
โ ๏ธ Warning: Local variables are not initialized automatically โ they contain garbage values until they are assigned a value.
Local Scope
A local variable is one declared inside a function, control structure, or block of code (between { }). It can only be used within that block and ceases to exist as soon as it ends.
#include <stdio.h>
int aux(){
int a = 9;
int b = 123;
}
int main(){
int a = 4;
}
Notice that in the code above, the declaration of aux
, we have that this variable with this value only exists and can be accessed within this function. That is, its scope is local to the aux
function. The same occurs for the variable main
only exists and can be accessed within that function.
With this in mind, analyze the output of the code below:
#include <stdio.h>
int aux(){
int a = 9;
int b = 123;
// prints the values of variables a and b contained in the aux function
printf("aux -> %d %d\n", a, b);
}
int main(){
int a = 4;
// prints the value of variable a contained in the main function
printf("Main -> %d\n", a);
// calls the aux function
aux();
}
/* Output:
Main -> 4
aux -> 9 123
*/
Observations:
- If, in the code above, we had tried to access the variable
inside the main
function, the compiler would alert us, warning thatwas not declared. - Structures like
if
,else
,while
, among others, also have a local scope. That is, a variable declared inside awhile
loop only exists and can be accessed within that structure.
Having learned this, we can understand what function parameters are. The parameters that are passed to a function represent local variables that must be initialized right when the function is called.
To make it easier to understand, analyze the code below:
#include <stdio.h>
int soma(int a, int b){
return a+b;
}
int main(){
printf("%d\n", soma(2, 5));
}
/* Output:
7
*/
In the code above, the variables
Global Scope
Variables with a global scope exist and can be accessed and modified in any part of the program. These variables must be declared outside of any function. Additionally, global variables are automatically initialized to zero, even if you do not assign a value to them.
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
When a local variable has the same name as a global variable, the local one has priority within its scope. This is called shadowing. To make it easier to understand, 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 aux
function.
When executing the first printf
command in main
, the program printed the value
When the aux
function is called, a local variable printf
command prints this same value, because the local variable
Upon returning to main
, the second printf
command is executed and prints the value aux
function did not change the value of the global variable, but created a local one and assigned another value to it.
Observation: notice that if we had not included the type (int
) before the name aux
function, we would not be declaring a new variable, but changing the value of the 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
*/
Variable scope can be a bit confusing at first, but you'll get the hang of it naturally as you solve more exercises ๐
Conclusion
Throughout this lesson, you learned what the scope of a variable is and how it defines where a variable can be accessed within the program. You saw the difference between local and global scope, understood that local variables exist only within the function or structure where they were declared, while global variables can be used in any part of the code and are naturally initialized with the value zero. You also understood the concept of priority, observing that local variables take precedence over global variables with the same name.
Now you know how to control access to variables, avoid conflicts between scopes, and write safer, more organized, and easier-to-understand programs ๐