#include<stdio.h>
int check,check;
void main(){
printf("Hello!!");
}
当我编译这段代码时,一切都正常,但是当我在主函数中给出它时,
#include<stdio.h>
void main(){
int check,check;
printf("Hello!!");
}
我收到类似的错误
C:\MinGW\bin>cc ex1.c
ex1.c: In function 'main':
ex1.c:4:11: error: redeclaration of 'check' with no linkage
int check,check;
^
ex1.c:4:5: note: previous declaration of 'check' was here
int check,check;
^
为什么会这样?
最佳答案
这在全局范围内是可能的,因为这被认为是一个暂定定义。
引用C11
,第 §6.9.2 章
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier
static
, constitutes a tentative definition.
但是,在 block 作用域(函数作用域)中,没有暂定定义,因此代码尝试创建两个具有相同名称的标识符,这是导致重新声明错误,因为它们实际上表示同一个对象。
您可以阅读更多相关信息 here .
也就是说,从 C99
开始,对于托管环境,void main()
不再是有效签名,它必须是 int main(void)
至少。
关于c - c中的全局变量声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773485/