In C++, variables can be: local, global, and dynamic. We will only discuss local and global variables.
Local variables are declared within a certain block of the program, in the body of a function. Consider the following program:
#include < iostream >
using namespace std;
void F()
{
int x;
x = 5;
cout << x << endl;
}
int main()
{
int y = 10;
F();
cout << y << endl;
return 0;
}
The variables x and y declared in the above program are local. The variable x can only be used in the function F(), and the variable y only in the function main(). Moreover, the two variables could have had the same name and there would have been no confusion.
Local variables follow these rules:
Note: The formal parameters of a function follow the properties of a local variable. We cannot declare variables in the function block with the same name as the formal parameters.
Global variables are declared outside any function. Upon declaration, they are initialized to 0.
#include < iostream >
using namespace std;
int x;
void F()
{
cout << x << endl;
x = 10;
}
int y;
int main()
{
cout << x << " " << y << endl;
x = 5; y = 15;
F();
cout << x << " " << y << endl;
return 0;
}
In the above program, the variables x and y are global. The variable x can be used both in the function main() and in F(), while the variable y only in main().
Global variables follow these rules:
Note: In a program, we can have both global and local variables, even global and local variables with the same name. The following program exemplifies this situation. Run it and analyze the displayed values to deduce how they are visible!
#include < iostream >
using namespace std;
int x;
void F()
{
cout << x << endl; //5, global variable
int x = 10;
cout << x << endl; //10, local variable in F()
{
int x = 20;
cout << x << endl; //20, local variable in F(), inner block
}
cout << x << endl; //10, local variable in F()
}
int y;
int main()
{
cout << x << endl; //0, global variable
x = 5;
cout << x << endl; //5, global variable
F();
cout << x << endl; //5, global variable
int x = 100;
cout << x << endl; //100, local variable in main()
return 0;
}
Note: If a program has variables with the same name but different visibility scopes, the variable with the smallest visibility scope takes precedence. In particular, if there is a global variable and a local variable with the same name, the local variable takes precedence.
Note: If we declare a variable in the initialization expression of a for statement, it will only be visible in the control expressions of the for statement and in the block subordinated to it. For example:
for(int i =1 ; i <= 10 ; i ++)
cout << i << " " ;
In this section you can generate a summary of the page content using AI! Feel free to use the button below whenever you are in a hurry and don't have time to learn everything!
In this section you can ask our expert robot anything related to the questions you encountered during the lessons! Feel free to use the button below whenever you need additional explanations!