请问C支持范围解析? [英] Does C supports scope resolution?
本文介绍了请问C支持范围解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main ()
{
/* local variable declaration */
int g = 10;
printf ("value of g = %d %d\n", g,::g);
return 0;
}
当我试图运行此程序。它抛出误差的main.c:11:39:错误:预期前$ P $之前pssion':'令牌
。但如果是用C ++编写,它工作正常。
的printf(G值=%D \\ N,G :: G);
When i am trying to run this program. It is throwing error main.c:11:39: error: expected expression before ':' token
printf ("value of g = %d %d\n", g,::g);
. But if it is written in C++, it works fine.
推荐答案
没有,这是一个C ++的功能。在C中,在内部范围内声明一个变量,将隐藏一个在外部范围。
No, that's a C++ feature. In C, declaring a variable in an inner scope will hide the one in the outer scope.
如果您的必须的做到这一点,你可以用指针来获得在外部范围,但它是一个有点杂牌的,这也不是我建议:
If you must do it, you can use pointers to get at the outer scope but it's a bit of a kludge and not something I'd recommend:
#include <stdio.h>
int g = 20;
int main () {
int *pGlobalG = &g;
int g = 10;
printf ("value of g = %d %d\n", g, *pGlobalG);
return 0;
}
这篇关于请问C支持范围解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文