开发者

how refer to a local variable share same name of a global variable in C? [duplicate]

This question already has answers here: How to print value of global variable and local variable having same name? 开发者_JAVA百科 (4 answers) Closed 5 years ago.

for example

#include<stdio.h>

int foo = 100;

int bar()
{
    int foo;
    /* local foo = global foo, how to implemented? */
    return 0;
}

int main()
{
    int result = bar();
    return 0;
}

I think in the function bar, calling foo directly will just get the global foo. How can I refer the local foo? I know in C++, there is this pointer. However, does C has something similar?

Thanks a lot!


No, by declaring foo in bar(), you have taken the global foo out of scope. Inside bar() when you refer to foo you get the local variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜