in C Plus Plus by
Q:

When there are a Global variable and Local variable with the same name, how will you access the global variable?

1 Answer

0 votes
by
When there are two variables with the same name but different scope, i.e. one is a local variable and the other is a global variable, the compiler will give preference to a local variable.

In order to access the global variable, we make use of a “scope resolution operator (::)”. Using this operator, we can access the value of the global variable.

Example:

#include<iostream.h>

int x= 10;

int main()

{

 int x= 2;

 cout<<”Global Variable x = “<<::x;

 cout<<”\nlocal Variable x= “<<x;

}

Output:

Global Variable x = 10

local Variable x= 2
Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

0 votes
asked Apr 13, 2020 in QuickTest Professional (QTP) by rajeshsharma
0 votes
asked Dec 4, 2020 in Amazon Elastic Compute Cloud EC2 by sharadyadav1986
0 votes
asked Jan 1 in Python by SakshiSharma
0 votes
asked Mar 17, 2020 in C Plus Plus by SakshiSharma
0 votes
asked Feb 17, 2020 in Ansible by MBarbieri
...