c - c中的全局变量声明

标签 c global-variables declaration variable-declaration redeclaration

#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/

相关文章:

c - 用ARM汇编语言编写一个函数,将一个字符串插入到另一个字符串的特定位置

c - 在 Xeon-Phi 上运行 Haskell

php - 没有错误,但页面仍然空白并传递变量

javascript - 如何在测试后期重用产生的值

function - 函数定义、调用和声明中的变量名

c++ - 如何枚举 vfs c 或 c++ 目录中的所有文件?

java - 在 C/C++/Java 中查找 USB 设备的信息

ruby - 你如何在 Ruby 中使用全局变量或常量值?

variables - 如何在 Lua 中声明一个变量而不将其初始化为 nil?

c - 返回指向静态局部变量的指针