c - visual studio 中不允许重复的外部静态声明?

标签 c visual-studio gcc static

我正在研究声明和定义的概念(链接、范围、持续时间)。

但是我发现了一个无法解释的错误:

以下代码在 gcc 和 visual studio 2010 中都没有问题

#include <stdio.h>

extern int a = 7;
extern int a;

int main()
{
    printf("%d\n", a);
}

但是下面的代码在 visual studio 中会产生错误,但在 gcc 中却没问题:

#include <stdio.h>

static int a = 7;
static int a;

int main()
{
    printf("%d\n", a);
}

error C2370: 'a' : redefinition; different storage class

这只是 visual studio 编译器中的一个错误吗?

编辑:这个问题原来是 this 的重复问题.

最佳答案

static int a; 本身没有初始化器是一个“暂定定义”,所以应该没问题。看起来 Microsoft 有某种扩展可以吸引您。

编辑 - 它看起来确实像 Microsoft 的问题。看看这个 related question . C 规范本身很清楚您的代码很好。来自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. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

关于c - visual studio 中不允许重复的外部静态声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8146541/

相关文章:

c - 声明 C 函数只接受常量

c# - 比较 float 的总和

visual-studio - VS 2013 Express for Desktop 无法打开 vcxproj 项目文件

c++ - 可见性生效时如何导出公共(public)内部/嵌套类?

c++ - 如何在 OSX 上安装 gtk 以与 g++/gcc 编译器一起使用

gcc - 强制gfortran在第一个NaN停止程序

在 C 中将二进制格式字符串转换为 int

c - 如何从 send 迁移到 writev?

检查一个矩阵是否包含另一个矩阵

C++:没有重载函数 unordered_set 的实例