473,804 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing a global variable when there is a local variable in the same name

int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

Regards,
Mohan.
Nov 14 '05
44 2043
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <9b************ *************@p osting.google.c om>
mo************@ msn.com (Mohanasundaram ) writes:
>int i = 10;
>int main()
>{
> int i = 20;
> return 0;
>}
>
>Hi All,
>
> I want to access the global variable i inside the main. Is there
>a way to do this in C?


Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^ issue should NEVER arise in real C programs. ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
#include <stdio.h>

int i = 10;

int main()
{
int j = i;
int i = 20;
printf("%d\n", j);
return 0;
}

If the value of the global i might change during the execution of main(),
replace the definition of j by

int *p = &i;

but keep it still *before* the definition of the local i, and use *p any
time you need to access the global i.


Yes, that will work. It will also create a potential headache for
future maintainers. Code that silently changes behavior when the
order of declarations is changed is not generally a good idea.


Which part of "This issue should NEVER arise in real C programs." was too
difficult for you to understand?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <9b************ *************@p osting.google.c om>
mo************@ msn.com (Mohanasundaram ) writes:
>int i = 10;
>int main()
>{
> int i = 20;
> return 0;
>}
>
>Hi All,
>
> I want to access the global variable i inside the main. Is there
>a way to do this in C?

Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^ issue should NEVER arise in real C programs. ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
#include <stdio.h>

int i = 10;

int main()
{
int j = i;
int i = 20;
printf("%d\n", j);
return 0;
}

If the value of the global i might change during the execution of main(),
replace the definition of j by

int *p = &i;

but keep it still *before* the definition of the local i, and use *p any
time you need to access the global i.


Yes, that will work. It will also create a potential headache for
future maintainers. Code that silently changes behavior when the
order of declarations is changed is not generally a good idea.


Which part of "This issue should NEVER arise in real C programs." was too
difficult for you to understand?


I understood it quite well, and I agree with it.

You declared that "This issue should NEVER arise in real C programs."
You could have stopped there, but you chose to present a solution.
That's fine. I commented on your solution. I agreed that it would
work, and raised another point that supports your contention that
"This issue should NEVER arise in real C programs."

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
>Da*****@cern.c h (Dan Pop) writes:
>> In <9b************ *************@p osting.google.c om>
>> mo************@ msn.com (Mohanasundaram ) writes:
>> >int i = 10;
>> >int main()
>> >{
>> > int i = 20;
>> > return 0;
>> >}
>> >
>> >Hi All,
>> >
>> > I want to access the global variable i inside the main. Is there
>> >a way to do this in C?
>>
>> Explain why you can't rename one of the two variables.
>>
>> If this is a homework question, try finding a better instructor. This

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^
>> issue should NEVER arise in real C programs.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
>>
>> #include <stdio.h>
>>
>> int i = 10;
>>
>> int main()
>> {
>> int j = i;
>> int i = 20;
>> printf("%d\n", j);
>> return 0;
>> }
>>
>> If the value of the global i might change during the execution of main(),
>> replace the definition of j by
>>
>> int *p = &i;
>>
>> but keep it still *before* the definition of the local i, and use *p any
>> time you need to access the global i.
>
>Yes, that will work. It will also create a potential headache for
>future maintainers. Code that silently changes behavior when the
>order of declarations is changed is not generally a good idea.


Which part of "This issue should NEVER arise in real C programs." was too
difficult for you to understand?


I understood it quite well, and I agree with it.

You declared that "This issue should NEVER arise in real C programs."
You could have stopped there, but you chose to present a solution.


A solution to a *homework question*, which you arbitrarily chose to treat
as advice about how to write real C programs, otherwise your comment about
future maintainers would be downright idiotic: homework answers have no
future maintainers.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:

[snip]
I understood it quite well, and I agree with it.

You declared that "This issue should NEVER arise in real C programs."
You could have stopped there, but you chose to present a solution.


A solution to a *homework question*, which you arbitrarily chose to treat
as advice about how to write real C programs, otherwise your comment about
future maintainers would be downright idiotic: homework answers have no
future maintainers.


Who said it was a homework question? The OP didn't say so. It's
entirely possible that it was homework; it's also possible that the OP
was trying to find out whether C has some kind of scope resolution
mechanism, as some other languages do.

I didn't treat your solution as advice about how to write real C
programs, but others might. In any case, I was simply expanding on
what you wrote. I don't know why you have a problem with that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:[snip]
>I understood it quite well, and I agree with it.
>
>You declared that "This issue should NEVER arise in real C programs."
>You could have stopped there, but you chose to present a solution.


A solution to a *homework question*, which you arbitrarily chose to treat
as advice about how to write real C programs, otherwise your comment about
future maintainers would be downright idiotic: homework answers have no
future maintainers.


Who said it was a homework question? The OP didn't say so. It's
entirely possible that it was homework;


My own text, prefacing the actual code, made it crystal clear that it was
supposed to be the answer to a stupid homework question and NOT used in
real life C code:

Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This
issue should NEVER arise in real C programs.
it's also possible that the OP
was trying to find out whether C has some kind of scope resolution
mechanism, as some other languages do.
And the answer is that it does (as shown in the various posted examples),
but it's better left unused.
I didn't treat your solution as advice about how to write real C
programs, but others might.
Why? Was my disclaimer clear enough for you, but not for others?
In any case, I was simply expanding on
what you wrote. I don't know why you have a problem with that.


Because I hate idiotic expansions on what I write. As I have already
explained, it makes no sense to invoke the future maintainers of something
that is *explicitly* presented as "not to be done in real C programs".

Without my explicit warning, your expansions would have been perfectly
sensible.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:

[...]
In any case, I was simply expanding on
what you wrote. I don't know why you have a problem with that.


Because I hate idiotic expansions on what I write.


Dan, it's really not that big a deal. What I wrote may not have been
necessary; your overreaction to it certainly wasn't.

In the future, I will spend less time worrying about your opinion
other than on relevant technical issues. I'll probably continue to do
things that make you angry (because there doesn't seem to be any way
to avoid that other than leaving the newsgroup). You can deal with
that in whatever way you choose.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16
Hi Dan,

This was an interview question. It is not a homework question. I
just wanted to know whether it is possible to do that like what we do
in C++. We use :: operator in C++ to access the global variable. I
wanted to know is there any language support in C to do this. If you
know please share the answer. If not please ......

I though the answer is either
There is no language support in C
or
There is an operator or something similar to do so like ::

Mohan.
Nov 14 '05 #17
Hi Thomson,

I feel you make lot of sense and show maturity in the full
thread. You are right and I wanted to know whether C supports any
thing similar to :: for accesing global varibles in C++. It was not a
homework question. I decided to ask this in this group because I had
and argument with an interviewer.

Regards,
Mohan.
Nov 14 '05 #18
In <9b************ *************@p osting.google.c om> mo************@ msn.com (Mohanasundaram ) writes:
Hi Dan,

This was an interview question. It is not a homework question. I
just wanted to know whether it is possible to do that like what we do
in C++. We use :: operator in C++ to access the global variable. I
wanted to know is there any language support in C to do this. If you
know please share the answer. If not please ......
Are you a patent idiot or what? I have already provided the answer.
Or are you unable to read standard C code?

There was no mention of the C++ :: operator in your initial question,
was it?
I though the answer is either
There is no language support in C
or
There is an operator or something similar to do so like ::


And you were wrong. There is language support in C, but it doesn't
involve any operator.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #19
Da*****@cern.ch (Dan Pop) writes:
In <9b************ *************@p osting.google.c om>
mo************@ msn.com (Mohanasundaram ) writes:
Hi Dan,

This was an interview question. It is not a homework question. I
just wanted to know whether it is possible to do that like what we do
in C++. We use :: operator in C++ to access the global variable. I
wanted to know is there any language support in C to do this. If you
know please share the answer. If not please ......


Are you a patent idiot or what? I have already provided the answer.
Or are you unable to read standard C code?

There was no mention of the C++ :: operator in your initial question,
was it?


The intent of Mohanasundaram' s original question was to ask whether C
has an operator that provides access to declarations in outer scopes
that are otherwise hidden. (That wasn't entirely clear from his
original question, but it's very clear now that he's come back and
clarified it.) There are such operators in some other languages (C++
and Perl have "::", Ada has ".", and I'm sure there are plenty of
other examples). The question might have been clearer if he had
mentioned C++'s "::" operator, but this is, after all, a C newsgroup.

The underlying question was actually a very good one, regardless of
the fact that the answer happens to be "no". And I'd recommend to
other posters, particularly newbies, that if you see us going off
track in discussing something you've asked about, please jump in early
and clarify what you really meant.
I though the answer is either
There is no language support in C
or
There is an operator or something similar to do so like ::


And you were wrong. There is language support in C, but it doesn't
involve any operator.


The solution Dan Pop provided was not so much a feature of C as a
workaround for the lack of a feature in C. It was a reasonable answer
to the original question as stated, but it didn't answer the intended
question. Yes, as I said, the original question could have been
worded more clearly; it took me a while to figure out the point
myself, and I'm not criticizing Dan for missing it.

I think most of the regulars here have either stopped reading Dan
Pop's postings or have learned to apply a mental "Dan Pop filter",
reading for technical content (which is usually very good) and
ignoring the abuse.

I hope that not too many newbies are put off by Dan Pop's appalling
rudeness, which I expect he'll demonstrate again in response to this
post.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
5157
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from the days before PHP got object-oriented features. For instance we currently have:
6
2109
by: Fernando Rodríguez | last post by:
Hi, I haven't used Python in quite some time, and I'm bit puzzled by this: counter = 0 class Blah(object): def run(self): counter += 1
2
2396
by: C Gillespie | last post by:
Dear All, I have 2 arrays var A1 = new Array(); A1 ="Y2"; var B1 = new Array(); B1 ="Y1"; B1 ="sink";
11
2574
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
9
356
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
9
3037
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29392
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
112
5496
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
4
3724
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. Example #include<something.h> int countVal = 100; /* Some stuff */
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.