为什么有初始化为零的所有剩余值这个数组? [英] Why is this array having all remaining values initialized to zero?

查看:159
本文介绍了为什么有初始化为零的所有剩余值这个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在 C语言程序设计,最近我开始学习阵列初学者,我研究过,默认情况下在一个int数组中的所有值都垃圾

Hello I am a beginner in C programming language, recently i started learning arrays, I have studied that by default all values in an int array are garbage.

那为什么我在这两种情况下获得不同的值。

Then why i am getting different values in these two cases.

案例1

int arr[5];

在这种情况下,将arr [0],直到ARR [4],我们将有垃圾值,但在接下来的情况。

in this case from arr[0] till arr[4] we will have garbage values, but in next case.

案例2

int arr[5] = {1};

在这种情况下,ARR [0]将具有值1并从改编[1]给Arr其余[4]将具有值0。

in this case arr[0] will have a value 1 and remaining from arr[1] to arr[4] will have value 0.

我的问题是,当案例1每一未initilized阵列位置有垃圾valeus那么为什么在案例2其余未initilized阵列位置有0作为默认值。

My question is that, When in case-1 each un-initilized array locations are having garbage valeus then why in case-2 remaining un-initilized array locations are having 0 as default value.

推荐答案

C11 6.7.9初始化P19 的包括这(我的重点)

C11 6.7.9 Initialization p19 covers this (my emphasis)

初​​始化应发生在初始化列表中的顺序,提供了一个特定的子对象覆盖所有各初始化
  previously上市初始化为相同的子对象; 151)全部
  未明确初始化子对象应被初始化
  隐含一样具有静态存储持续时间的对象

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

部分的 6.7.9 P10 的规定,

如果具有静态或线程存储持续时间的目的是不
  显式初始化,那么......如果有算术类型,它是
  初始化(正或无符号)为零;

If an object that has static or thread storage duration is not initialized explicitly, then...if it has arithmetic type, it is initialized to (positive or unsigned) zero;

这篇关于为什么有初始化为零的所有剩余值这个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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