数组的默认值是多少?
我正在创建一个数组表,用户可以在其中自己输入值,并且我将向他们显示值表,但数字 0
用于退出 scanf
所以我不希望数字 0
存储在数组中
,但我看到数字 0
作为数组的下一个值,所以我想知道是否0
是数组的默认值。
为了让它更清楚一点,
假设用户输入了;
5 4 3 2
1
和 0
所以我想在程序输出中显示的是
5 4 3 2 1
我使用数组来显示 0-4 [1-5] 的索引
,但只是为了确保数字 0
是否未存储,因此我调用 array[5] 来查看下一个值是否为0
或其他内容,它总是显示 0
所以我想知道是否有办法确保 0
不会存储在数组中,
这是我用来确保
if(enter != 0){
array[i - 1] = Enter;
抱歉,如果我的问题很复杂。
谢谢
I'm creating a table of an arrays where the user can input the value themselves, and I will show them the table of value except the number 0
is use to exit from the scanf
so I don't want number 0
to be store in the array
but I'm seeing number 0
for the next value of an array so I'm wondering if 0
is a default value of an array.
to make it a bit more clear
let say the user entered;
5
4
3
2
1
and 0
so what I suppose to show in my program output is
5
4
3
2
1
which I use array to display the index of 0-4 [1-5]
but just to make sure if number 0
is not store so I call array[5] to see if the next value is 0
or something else and it always display 0
so I want to know if there is a way to make sure that 0
is not going to store in the array
this is what I use to make sure
if(enter != 0){
array[i - 1] = enter;
Sorry if my question is complicated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果数组具有静态存储持续时间,它将被初始化为 0。在任何其他情况下,元素将不会被初始化,即将包含随机位。
静态存储持续时间有全局变量、文件范围静态变量和块范围静态变量。
If the array is with static storage duration it will be initialized to 0. In any other case, elements will not be initialized, i.e. will contain random bits.
Static storage duration have global variables, file scope static variables and block scope static variables.
我修改我的答案。您分配的每个数组都可以包含垃圾,也可以不包含垃圾。编译器可以为您清除它,也可以不清除它。在你的情况下,我会用 -1 之类的值清除数组。否则你会在那里有一些未定义的值,即 0 或非 0。只是没有定义,每个编译器的行为都不同。
测试一下:
I revide my answer. Every array that you allocate can contain garbage, or not. The compiler can clear it for you, or not. In your case, I would clear the array with something like -1. Else you will have some undefined values in there, that be 0 or not. It's just not defined, every compiler can behave differently.
Test it:
这取决于情况,但通常在编译时定义的数组默认都是
0
。不一定是在运行时定义的数组,它们可能是 0 或充满垃圾。It depends, but generally arrays defined at compile-time are full of
0
by default. Arrays defined at run-time not necessarily, they could be0
or full of crap.