Can we access a global variable if there is a local variable with the same name in C?


Global Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

Example

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
  print("Python is " + x)

myfunc()

Try it Yourself »

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Example

Create a variable inside a function, with the same name as the global variable

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

Try it Yourself »



The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

Example

If you use the global keyword, the variable belongs to the global scope:

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

Try it Yourself »

Also, use the global keyword if you want to change a global variable inside a function.

Example

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

Try it Yourself »



Hi All, In C language , it is possible to access a global variable which has got same name of local variable. For eg: int a=10; main(){ fun(); } int fun(){ int a=5; /* I want to access global variable a which has got value of 10*/ a++; // } Please help out. Thanks. shekar

Read these next...

  • Can we access a global variable if there is a local variable with the same name in C?

    Low C Drive Space but not true

    Windows

    Hi Folks.I have a 2016 VM RDP Server that will not allow me to copy a 900MB file to the C drive.  The system reports 80.2GB free of 199GB.  I've run chkdsk, I've run Disk Cleanup, I've review the logs and found no relevant errors.  Also it's worthy to not...

  • Can we access a global variable if there is a local variable with the same name in C?

    Struggling to go from self taught to semi-proficient with WatchGuard firewalls

    Security

    Here's where I'm at. I'm pretty much the sole front line IT guy here at our facility. I used to be 1 person piece of a 3 person department with my primary role being programming customized programs in our ERP's SDK. But my background was general support f...

  • Can we access a global variable if there is a local variable with the same name in C?

    Confusing EDR pricing & pricing transparency is a big problem these days!!

    Security

    Hey fellow spiceheads!We are currently looking around for a new EDR product.Many EDR solutions out there look and sound very interesting to us, but they all come with the same issue. PRICING!!! or lack of pricing transparency!Is it too much to ask for com...

  • Can we access a global variable if there is a local variable with the same name in C?

    Snap! -- New Moon Race, Pink Auroras, T-Rex Auction, 3D-Printed Rocket Engine

    Spiceworks Originals

    Your daily dose of tech news, in brief. Welcome to the Snap! Flashback: Back on November 10, 1983, Microsoft announces a new product: Windows (Read more HERE.) Bonus Flashback: Back on November 10, 2001, Apple ships the first iPod (Read more ...

  • Can we access a global variable if there is a local variable with the same name in C?

    PowerShell 7.,3 is available!

    Programming & Development

    The PowerShell team have released the next version of PowererShell: 7.3. Like every update, this one has performance and functionality improvements and is a very useful upgrade but there are some things to be aware of. You can read the release notes here:...

Can we access global variable if there is a local variable with same name?

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.

Can local and global variables have the same name in C?

It is usually not a good programming practice to give different variables the same names. If a global and a local variable with the same name are in scope, which means accessible, at the same time, your code can access only the local variable.

How do you access global variable if there is a local variable with same name in Java?

Using Scope resolution operator (::): In C++, we can use the scope resolution operator (::) to access a global variable if we have a local variable with the same name.

How do you access global variable if there is a local variable with same name in Python?

It is also possible to use a global and local variable with the same name simultaneously. Built-in function globals() returns a dictionary object of all global variables and their respective values. Using the name of the variable as a key, its value can be accessed and modified.