Redian新闻
>
[转载] Help!! variable scope ????
avatar
[转载] Help!! variable scope ????# CS - 计算机科学
c*n
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;
/////////////////////////////////
avatar
f*p
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;

avatar
c*n
3
of course I know the results
just want to know the standards / or lack thereof

【在 f*****p 的大作中提到】
: Why not just try it out.
avatar
f*p
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

avatar
c*n
5
I always use
extern/static for sure,

【在 f*****p 的大作中提到】
: No idea. If this is an interview question, it's bad.
avatar
f*p
6
if u sure, why post?

【在 c******n 的大作中提到】
: I always use
: extern/static for sure,

avatar
c*n
7
it's still allowable by standards!
many people write such code

【在 f*****p 的大作中提到】
: if u sure, why post?
avatar
v*d
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;

avatar
c*n
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。