Accessing global variable in a local block

S

Shilpa

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;
printf("%d", temp) ;
}

Is it possible to access the global temp variable in the local block ?
If yes, please let me know.
 
K

Keith Thompson

Shilpa said:
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;
printf("%d", temp) ;
}

Is it possible to access the global temp variable in the local block ?
If yes, please let me know.

Not directly, no. You can save the address of the global variable in
a pointer and access it indirectly.

Or, better yet, if you have a global variable you want to access,
don't give the same name to a local variable.
 
C

Chris Dollin

Shilpa said:
Hi,
I just wanted to know whether we can access global variable within a
local block , where both variables are having same name.

Not by name, no. (You /could/ take the address of the global,
stuff it into a pointer, and then access the global through
that pointer. You /could/. You /could/ also push a pea up a
mountain with your nose.)
For ex:

int temp=5 ;

{
int temp=10;
printf("%d", temp) ;
}

Is it possible to access the global temp variable in the local block ?
If yes, please let me know.

Yes. Rename the local variable to something else.
 
E

Eric Sosman

Shilpa said:
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;
printf("%d", temp) ;
}

Is it possible to access the global temp variable in the local block ?
If yes, please let me know.

Yes, but only before the local `temp' is declared.

{
int dint = temp; /* 5 */
int temp = 10;
int lint = temp; /* 10 */
...

You could even do

{
int temp = temp; /* 5 */

.... but people who do any of these things deserve punishment.
 
L

Lew Pitcher

Keith said:
Not directly, no. You can save the address of the global variable in
a pointer and access it indirectly.

Keith,

Wouldn't it be possible to access the global "temp" from a local block
if the variable was defined as extern in that block? Something like

int temp=5;

{
int temp=10;
printf("%d\n",temp); /* should print 10 */

{
extern int temp;

printf("%d\n",temp); /* should print 5 ? /*
}
}
 
S

santosh

Shilpa said:
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;
printf("%d", temp) ;
}

Is it possible to access the global temp variable in the local block ?
If yes, please let me know.

Not by name since it is shadowed by the local. However you can still
access it by deferencing a pointer to it. In any case this is in almost
all situations bad programming practise.
 
L

Lew Pitcher

santosh said:
Not by name since it is shadowed by the local.

Hmmmm.... I'm still waiting for a definitive answer, but I believe that
you are incorrect.

This /may/ just be a local extension introduced by the implementation,
but I believe that it isn't.

pitchl@phantom:~/localcode/ctest$ cat ctest1.c
#include <stdio.h>
#include <stdlib.h>

int temp = 5;

int main(void)
{
int temp = 10;

printf("temp (inside main()) = %d\n",temp);

{
extern int temp;

printf("temp (inside local) = %d\n",temp);
}
return EXIT_SUCCESS;
}

pitchl@phantom:~/localcode/ctest$ cc -o ctest1 ctest1.c

pitchl@phantom:~/localcode/ctest$ ./ctest1
temp (inside main()) = 10
temp (inside local) = 5

pitchl@phantom:~/localcode/ctest$
 
S

santosh

Lew said:
Hmmmm.... I'm still waiting for a definitive answer, but I believe that
you are incorrect.

This /may/ just be a local extension introduced by the implementation,
but I believe that it isn't.

pitchl@phantom:~/localcode/ctest$ cat ctest1.c
#include <stdio.h>
#include <stdlib.h>

int temp = 5;

int main(void)
{
int temp = 10;

printf("temp (inside main()) = %d\n",temp);

{
extern int temp;

printf("temp (inside local) = %d\n",temp);
}
return EXIT_SUCCESS;
}

pitchl@phantom:~/localcode/ctest$ cc -o ctest1 ctest1.c

pitchl@phantom:~/localcode/ctest$ ./ctest1
temp (inside main()) = 10
temp (inside local) = 5

Looks like a case of double shadowing. Is this behaviour mentioned in
the standard somewhere?
 
K

Keith Thompson

Lew Pitcher said:
Keith,

Wouldn't it be possible to access the global "temp" from a local block
if the variable was defined as extern in that block? Something like

int temp=5;

{
int temp=10;
printf("%d\n",temp); /* should print 10 */

{
extern int temp;

printf("%d\n",temp); /* should print 5 ? /*
}
}

Sure. But any solution other than renaming the local variable (or
perhaps the global one) is just silly.

Also, the extern solution works for globals, but not for locals in
outer scopes.
 
C

Chris Torek

[long quote, sorry ... I saved a bit of vertical space by doing ugly things
with {}s and deleting white-space]

Looks like a case of double shadowing. Is this behaviour mentioned in
the standard somewhere?

Yes, the Standard requires the above program to print 10 and 5
on the corresponding lines.

Note that the behavior becomes undefined if the first declaration
for "temp" (line 3 in the edited version above) is changed to read
"static int temp = 5;", because then a single translation unit
(ctest1.c) uses the same identifier with both internal and external
linkage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top