Categories

Jun 9 in C++
Q:

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

1 Answer

Jun 10

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.

Click here to read more about C++
Click here to read more about Insurance

Related questions

Madanswer
Mar 10 in C++
Apr 13 in QuickTest Professional (QTP)
Apr 13 in QuickTest Professional (QTP)
...