C中的全局变量总是被初始化为零? [英] Are global variables always initialized to zero in C?

查看:129
本文介绍了C中的全局变量总是被初始化为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
int a[100];
int main(){
    printf("%d",a[5]);
    return 0;
}

以上代码是否总是打印0或者是编译器特定的?

Does the above code always print '0' or is it compiler specific? I'm using gcc compiler and I got the output as '0'.

推荐答案

是的,所有<$ c $的成员都使用gcc编译器, c> a 保证初始化为0。

Yes, all members of a are guaranteed to be initialised to 0.

从C89标准的3.5.7节开始

From section 3.5.7 of the C89 standard


如果一个具有静态存储持续时间的对象没有被显式初始化
,就会隐式初始化它,就好像每个具有
算术类型的成员都被赋值为0并且每个具有指针类型
的成员都被赋予一个空指针常量。

If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.

这篇关于C中的全局变量总是被初始化为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆