0 votes
in C Plus Plus by

What is the precedence when there are a Global variable and a Local variable in the program with the same name?

1 Answer

0 votes
by

Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.

Example:

#include <iostream.h>

 int globalVar = 2;

int main()

{

 int globalVar = 5;

 cout<<globalVar<<endl;

}

The output of the above code is 5. This is because, although both the variables have the same name, the compiler has given preference to the local scope.

...