69,371
社区成员
发帖
与我相关
我的任务
分享
int x;
void f (void)
{
extern int x;
if (/* ... */)
{
extern int x;
}
}
[/quote]
楼主是要干嘛?[/quote]
我就是想问,这3个x是不是同一个。int x;
void f (void)
{
extern int x;
if (/* ... */)
{
extern int x;
}
}
[/quote]
楼主是要干嘛?int x;
void f (void)
{
extern int x;
if (/* ... */)
{
extern int x;
}
}
$ echo 'static int i;' > si.c
$ echo 'extern int i;' > ei.c
$ cat si.c ei.c > sei.c
$ cat ei.c si.c > esi.c
$ gcc - c sei.c
$ gcc - c esi.c
esi.c:2 : 12 : error : static declaration of ‘i’ follows non - static declaration
static int i;
^
esi.c:1 : 12 : note : previous declaration of ‘i’ was here
extern int i;
^
If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.
For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
xx.h里有 extern int data_handle; a.c里有static int data_handle, 其他.c也用到了data_handle, 其他.c里有没有用static修饰该变量,我没有关注!!!! 我的问题是: 如果a.c包含 xx.h,那么 data_handle; 会有什么后果? a.c如果没有包含xx.h,那么data_handle又有什么后果? 我想对a.c里的static int data_handle;重命名,由于没有搞定这语法,所以不敢随意重命名。
static 声明的全局变量只能在当前源文件中使用; extern 不是定义,是引入(声明)在其它源文件中定义的非 static 全局变量;