#include<stdio.h>
int x=13; // forcing space allocation
int x;
int main(){
printf("%d\n",x);
}
上面的代码可以编译,但下面的代码不能编译。为什么 ?
#include<stdio.h>
int main(){
int x=13; // forcing space allocation
int x;
printf("%d\n",x);
}
有人告诉我 int x ;编译器可以根据上下文将其解释为声明或定义。我可以看到第一种情况(全局情况),但第二种情况会发生什么。
Quoting: https://stackoverflow.com/questions/2068798/how-to-access-the-correct-global-variable-in-c/2068819#2068819
C 程序中不能有两个同名的全局变量。 C
可能允许通过以下方式在同一文件范围内进行多个定义暂定定义规则 http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03tentative_defn.htm,但无论如何所有定义都会引用
到同一个变量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)