How to Access Global Variable if there is a Local Variable with Same Name in C/ C++? Last Updated : 21 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Local Variable: The variable whose scope lies inside a function or a block in which they are declared. Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolution operator respectively. In C: 1) We can access a global variable if we have a local variable with same name in C using extern. C // C Program to demonstrate that we can access a global // variable if we have a local variable with same name #include <stdio.h> // Global variable x int x = 50; int main() { // Local variable x int x = 10; { extern int x; printf("Value of global x is %d\n", x); } printf("Value of local x is %d\n", x); return 0; } OutputValue of global x is 50 Value of local x is 10 Time Complexity: O(1) Auxiliary Space: O(1) In C++: 2) We can access a global variable if we have a local variable with the same name in C++ using Scope resolution operator (::). C++ // C++ Program to demonstrate that We can access a global // variable if we have a local variable with same name in // C++ using Scope resolution operator (::) #include <iostream> using namespace std; // Global variable x int x = 50; int main() { // Local variable x int x = 10; cout << "Value of global x is " << ::x << endl; cout << "Value of local x is " << x; getchar(); return 0; } OutputValue of global x is 50 Value of local x is 10 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article lvalues references and rvalues references in C++ with Examples K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads How to return local variables from a function in C++ In C++, returning a local variable from a function may result in an error due to the deallocation of variable memory after some value is returned from the function. But there are some ways using which we can safely return the local variables or the address of local variables and manipulate the data 4 min read lvalues references and rvalues references in C++ with Examples Prerequisites: lvalue and rvalue in C++, References in C++âl-valueâ refers to a memory location that identifies an object. "r-valueâ refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declare 4 min read Can References Refer to Invalid Location in C++? Reference Variables: You can create a second name for a variable in C++, which you can use to read or edit the original data contained in that variable. While this may not sound appealing at first, declaring a reference and assigning it a variable allows you to treat the reference as if it were the 2 min read Can Global Variables be dangerous ? In a small code, we can track values of global variables. But if the code size grows, they make code less understandable (hence less maintainable). It becomes difficult to track which function modified the value and how. C++ // A C++ program to demonstrate that a // global variables make long code l 2 min read How to Get a Unique Identifier For Object in C++? Prerequisite: Classes and Objects in C++ A single entity within a given system is identified by a string of numbers or letters called a unique identifier (UID). UIDs enable addressing of that entity, allowing access to and interaction with it. There are a few choices, depending on your "uniqueness" 3 min read Printing the Address of an Object of Class in C++ Prerequisite: Classes and Objects in C++ The location of an object in the memory is called its address. Addressing is a necessary part of C++, it enables us to use any element as a reference and maintains the uniqueness of all the elements whether it is any variable, object, or container. In this ar 3 min read Like