数组的默认值是多少?

时间:2021-11-08 17:09:19

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

我正在创建一个数组表,用户可以在其中输入值本身,我将向他们显示值表,但数字0用于从scanf中退出

so I don't want number 0 to be store in the array

所以我不希望0存储在数组中

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.

但是我看到一个数组的下一个值是0所以我想知道0是否是一个数组的默认值。

to make it a bit more clear

让它更清楚一点。

let say the user entered;

假设用户输入;

5 4 3 2

5 4 3 2

1

and 0

和0

so what I suppose to show in my program output is

我想在程序输出中显示的是。

5 4 3 2 1

5 4 3 2 1。

which I use array to display the index of 0-4 [1-5]

我使用数组来显示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

但为了确保如果数字0不是商店所以我把数组[5],看看下一个值为0或别的东西,它总是显示0所以我想知道如果有一种方法可以确保0不会存储在数组中

this is what I use to make sure

这是我用来确定的

if(enter != 0){

如果(输入! = 0){

array[i - 1] = enter;

数组[i - 1] =输入;

Sorry if my question is complicated.

抱歉,我的问题很复杂。

Thanks

谢谢

3 个解决方案

#1


1  

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.

我revide回答。您分配的每个数组都可以包含垃圾,或者不包含垃圾。编译器可以替你清除,也可以不清除。在你的例子中,我要用-1来清除数组。否则会有一些未定义的值,是0还是0。只是没有定义,每个编译器的行为都是不同的。

Test it:

测试:

#include <stdio.h>


void array(void);

int main(void) {
    int c[10], i;
    printf("Array 1: ");
    for(i = 0; i < 10; i++) {
        printf("%d", c[i]);
    }

    printf("\n");
    array();
    int b[10];
    for(i = 0; i < 10; i++) {
        printf("%d ",b[i]);
    }

    printf("\n");

    return 0;
}

void array(void) {
    int a[10] = {1,2,3,4,5,6,7,8,9,0};
    return;
}

#2


4  

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.

如果数组具有静态存储时间,则将初始化为0。在任何其他情况下,元素都不会初始化,即包含随机位。

Static storage duration have global variables, file scope static variables and block scope static variables.

静态存储期间有全局变量、文件范围静态变量和块范围静态变量。

#3


1  

It depends, but generally arrays defined at compile-time are full of 0 by default. Arrays defined at run-time not necessarily, they could be 0 or full of crap.

这要看情况,但通常在编译时定义的数组在默认情况下都是0。在运行时定义的数组不一定,它们可以是0或充满垃圾。

#1


1  

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.

我revide回答。您分配的每个数组都可以包含垃圾,或者不包含垃圾。编译器可以替你清除,也可以不清除。在你的例子中,我要用-1来清除数组。否则会有一些未定义的值,是0还是0。只是没有定义,每个编译器的行为都是不同的。

Test it:

测试:

#include <stdio.h>


void array(void);

int main(void) {
    int c[10], i;
    printf("Array 1: ");
    for(i = 0; i < 10; i++) {
        printf("%d", c[i]);
    }

    printf("\n");
    array();
    int b[10];
    for(i = 0; i < 10; i++) {
        printf("%d ",b[i]);
    }

    printf("\n");

    return 0;
}

void array(void) {
    int a[10] = {1,2,3,4,5,6,7,8,9,0};
    return;
}

#2


4  

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.

如果数组具有静态存储时间,则将初始化为0。在任何其他情况下,元素都不会初始化,即包含随机位。

Static storage duration have global variables, file scope static variables and block scope static variables.

静态存储期间有全局变量、文件范围静态变量和块范围静态变量。

#3


1  

It depends, but generally arrays defined at compile-time are full of 0 by default. Arrays defined at run-time not necessarily, they could be 0 or full of crap.

这要看情况,但通常在编译时定义的数组在默认情况下都是0。在运行时定义的数组不一定,它们可以是0或充满垃圾。