喵喵锤锤你小可爱 2022-01-19 20:34 采纳率: 50%
浏览 23
已结题

有精通C标准的吗,为什么么一个文件里面一个全局变量初始化过后还可以在定义未初始化的

如图所示,为什么还能编译过?

#include <stdio.h>
int aaa=0,bbb=0,ccc=0;
int aaa,bbb,ccc;
int aaa,bbb,ccc;
int aaa,bbb,ccc;
int main()
{
   /*  Write C code in this online editor and run it. */
   printf("Hello, World! \n");
   aaa = 10;
   return 0;
}

img

  • 写回答

2条回答 默认 最新

  • _GX_ 2022-01-19 22:43
    关注

    代码中int aaa, bbb, ccc;叫做暂定的定义,相当于声明。 这是为了兼容标准C语言之前的旧代码,那时候编译器把多个重复定义的全局变量合并成一个。

    #include <stdio.h>
    int aaa = 0, bbb = 0, ccc = 0;  // external definitions
    int aaa, bbb, ccc;  // tentative definitions, act as declarations
    int aaa, bbb, ccc;  // tentative definitions, act as declarations
    int aaa, bbb, ccc;  // tentative definitions, act as declarations
    int main()
    {
        /*  Write C code in this online editor and run it. */
        printf("Hello, World! \n");
        aaa = 10;
        return 0;
    }
    

    From https://en.cppreference.com/w/c/language/extern#Tentative_definitions

    A tentative definition is an external declaration without an initializer, and either without a storage-class specifier or with the specifier static.

    A tentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月31日
  • 已采纳回答 1月23日
  • 创建了问题 1月19日

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示