?
Solved

accessing global variable in c.

Posted on 2005-03-28
3
Medium Priority
?
231 Views
Last Modified: 2010-04-15
hello.
in c++ we have :: operator to acess global variable ( if we have a local variable with the same name as that of global variable).
BUT how does we access such a global variable in c?
e.g


int x;
main()
{
       int x;
      :
      :

}

how do i access global x in main()
0
Comment
Question by:soodsandeep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Expert Comment

by:SilkyShark
ID: 13642274
You can't with a 'normal' language construct.
You can by using pointers that point to the original variable but that is not really any point.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 200 total points
ID: 13642345
SilkyShark is right, you can't.

There are a number of work arounds though ... pointers, as already suggested, is one of them
Another would be to write a function which simply returns the value of the global variable

int FetchGlobalVar ()
{
  return (GlovalVar);
}

These and more ways + discussions on this very topic are here
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20722542.html
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13647703
Hi soodsandeep,
You shouldn't do this, anyway. It's not good programming style. Since C lacks C++'s namespaces, you'd normally prefix globals, or store them in a global structure - it's less pollution of the global namespace.

Cheers!

Stefan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question