c******n 发帖数: 4965 | 1 【 以下文字转载自 Programming 讨论区 】
【 原文由 creation 所发表 】
I think I read somewher before that
the scopes of global variables in C are not cleanly defined
if you have multiple definitions of a variable in different files,
some compilers take them to be "static", some take as "global"
e.g. I have a.c, and b.c
////////////////////////////
// a.c
int n;
void fun();
void main(){
n=1;
fun();
printf("main: %d\n", n);
}
//////////////
// b.c
int n;
void fun(){
n=2;
///////////////////////////////// |
f*****p 发帖数: 235 | 2 Why not just try it out.
【在 c******n 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 【 原文由 creation 所发表 】 : I think I read somewher before that : the scopes of global variables in C are not cleanly defined : if you have multiple definitions of a variable in different files, : some compilers take them to be "static", some take as "global" : e.g. I have a.c, and b.c : //////////////////////////// : // a.c : int n;
|
c******n 发帖数: 4965 | 3 of course I know the results
just want to know the standards / or lack thereof
【在 f*****p 的大作中提到】 : Why not just try it out.
|
f*****p 发帖数: 235 | 4 No idea. If this is an interview question, it's bad.
【在 c******n 的大作中提到】 : of course I know the results : just want to know the standards / or lack thereof
|
c******n 发帖数: 4965 | 5 I always use
extern/static for sure,
【在 f*****p 的大作中提到】 : No idea. If this is an interview question, it's bad.
|
f*****p 发帖数: 235 | 6 if u sure, why post?
【在 c******n 的大作中提到】 : I always use : extern/static for sure,
|
c******n 发帖数: 4965 | 7 it's still allowable by standards!
many people write such code
【在 f*****p 的大作中提到】 : if u sure, why post?
|
v******d 发帖数: 1322 | 8 if u don't use extern, u cannot access the global
variable defined in the other files.
【在 c******n 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 【 原文由 creation 所发表 】 : I think I read somewher before that : the scopes of global variables in C are not cleanly defined : if you have multiple definitions of a variable in different files, : some compilers take them to be "static", some take as "global" : e.g. I have a.c, and b.c : //////////////////////////// : // a.c : int n;
|
c******n 发帖数: 4965 | 9 just try my example with gcc
【在 v******d 的大作中提到】 : if u don't use extern, u cannot access the global : variable defined in the other files.
|