views:

127

answers:

7
# include <stdio.h>

int x = 5;
int main(void)
{

        int x = 7;
        printf("output = %d\n", x);

}

The above program shows output as 7. How to print 5 in c?

thanx...

A: 

If you want it 5, why you assign 7? :-) Or you want to access global variable with the same name as local one? Then you might use namespaces... Not sure about C :-)

BarsMonster
C doesn't have namespaces
Paul
@Paul: While I agree with the sentiment of calling out people who treat C as if it were C++, any language has a primitive sort of namespaces simply through naming. If you insist on having global variables, naming them `mylib_x` or `myproject_x` instead of just `x` will at least avoid one of the many problems they create.
R..
A: 

Delete the x = 7 line, move the print statement before you print the value or set a different variable to 7 (i.e. write y = 7 instead of x = 7).

Belinda
+3  A: 

Don't redeclare the x variable in the main function...

In fact, i'm sensing (perhaprs wrongly) that there's another question behind your code, but i'll keep my reaction to it short: if you want to mix global variables and local variables, try to have a convention to distinguish them; for example globals in ALL_CAPS to shout out its scope :)


EDIT: By the way, you should be getting at least a warning from your compiler for redefining a different scope/same name variable. it's really not recommended doing that. Try to always aim for minimum warnings...

samy
ALL_CAPS is usually used for preprocessor macros/constants. So using them for global variables is not a really good idea...
ThiefMaster
Using all caps for both global variables and macros doesn't conflict, as long as your number of global variables is limited to 0 as it should be. :-)
R..
@Thiefmaster: you've got a point; for a moment there i was in a different language than the C family ;) @R Global variables' use should really be discouraged, but i really think we should give some slack to people using them when appropriate (and i do know it takes talent to determine when they're appropriate :) )
samy
+6  A: 

So you're asking how to access a global that's shadowed by a local? You can't in that scope, but something like this should work

# include <stdio.h>

int x = 5;
int get_global_x()
{
  return x;
}

int main(void)
{

        int x = 7;
        printf("output = %d\n", get_global_x());

}
Paul
nice trick... didn't think of it
samy
Or add a global `int *ptr_to_x = ` and access the global `x` through that.
R..
@R +1 for the even better idea, but it really feels like giving a loaded gun to somebody not ready to use it :)
samy
Or make x a static local of get_global_x()
Patrick
Guys is it possible to declare the the same variable name to two values and retrieve only the global one(without the use of any function call)?? like we do in c++ with name spaces.
Pavitar
@Pavitar, No, it is not possible with C.
Paul
@ Paul : Thank you. +1
Pavitar
+1  A: 

You need to give your two variables meaningful names. I'm sure you aren't using x in your real code so do the two vars actually have the same meaning and therefore the same name? If so you could at least do (assuming your ints are counts, but works for all other purposes):

int global_countOfThings = 5;
int main(void)
{
    int countOfThings  = 7;
    printf("output = %d\n", global_countOfThings );    
}

But hopefully you'll be able to do something like:

int countOfDucks = 5;
int main(void)
{
    int countOfGeese  = 7;
    printf("output = %d\n", countOfDucks );    
}

And of course if you can some how change your code to not use globals you'll be better off in the long run.

Patrick
+1  A: 

You are declaring the same variable in global and local scope. If the same variable is declared in global and local scope, the variable will be treated as local variable. That's the reason you are getting the answer as 7.

Swapna
Local variable are given first precedence over global variable. That's the reason you are getting answer as 7 and not as 5.
Swapna
A: 

u have already declared x to behave as if it is a global variable.to print 5 u can write the pritf statement as: printf("%d\n",x-2); The above would print 5.

abcdef