Scope resolution in C

S

Subra

Hi,

If I have the same varible defined in global as well as in local
scope, how to access global scope varible in a function having the same
local def.

#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",a);
}
#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",a); // How to access the global def??
}
 
R

Richard Heathfield

Subra said:
#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",a); // How to access the global def??

By removing the local def.
 
C

CBFalconer

Subra said:
If I have the same varible defined in global as well as in local
scope, how to access global scope varible in a function having
the same local def.

#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",a);
}

You don't. You are perfectly free to change the name of the local
variable.
 
K

Kenny McCormack

Subra said:


By removing the local def.

Or by turning the computer off.

But seriously, folks. Every other time this thread has come up,
somebody has come up with the solution. I'm surprised it hasn't
happened here yet.

#include<stdio.h>
static int a=25;
int *getPointer2A() { return &a; }
int main()
{
int *ap = getPointer2A();
static int a = 10;

printf("local a = %d, global a = %d\n",a,*ap);
return 0; /* Just for you anal types; yes, I know about EXIT_SUCCESS */
}

(Untested; but the only thing I'm not sure about is the "static" - I
don't think that changes anything relevant, though)
 
G

Guest

Kenny said:
Or by turning the computer off.

But seriously, folks. Every other time this thread has come up,
somebody has come up with the solution. I'm surprised it hasn't
happened here yet.

#include<stdio.h>
static int a=25;
int *getPointer2A() { return &a; }
int main()
{
int *ap = getPointer2A();
static int a = 10;

printf("local a = %d, global a = %d\n",a,*ap);
return 0; /* Just for you anal types; yes, I know about EXIT_SUCCESS */
}

(Untested; but the only thing I'm not sure about is the "static" - I
don't think that changes anything relevant, though)

It seems okay to me, and you're right, in this case the "static" for
the second definition of a doesn't change anything. I don't see a need
for both a helper function and a pointer, though: you can remove
either.

#include <stdio.h>
static int a=25;
int main()
{
int *ap = &a;
static int a = 10;

printf("local a = %d, global a = %d\n",a,*ap);
return 0;
}

or

#include <stdio.h>
static int a=25;
int *getPointer2A() { return &a; }
int main()
{
static int a = 10;

printf("local a = %d, global a = %d\n",a,*getPointer2A());
return 0;
}
 

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,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top