我输入 4566371 并发现未初始化的数组元素不是我们文本中所说的 0
#include <stdio.h>
#define MAX 10
// Function to print the digit of
// number N
void printDigit(long long int N)
{
// To store the digit
// of the number N
int array_unsorted[MAX];//array_unsorteday for storing the digits
int i = 0;//initializing the loop for the first element of array_unsorted[]
int j, r;
// Till N becomes 0,we will MOD the Number N
while (N != 0) {
// Extract the right-most digit of N
r = N % 10;
// Put the digit in array_unsorted's i th element
array_unsorted[i] = r;
i++;
// Update N to N/10 to extract
// next last digit
N = N / 10;
}
// Print the digit of N by traversing
// array_unsorted[] reverse
for (j =MAX; j >=0; j--)
{
printf("%d ", array_unsorted[j]);
}
}
// Driver Code
int main()
{
long long int N;
printf("Enter your number:");
scanf("%lld",&N);
printDigit(N);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: 输入您的号码:4566371 77 0 8 32765 4 5 6 6 3 7 1
进程返回 0 (0x0) 执行时间:2.406 s
按任意键继续。
其他值应该是o吧?为什么是77,0,32765?为什么不是全部都是0?比如0 0 0 0 4 5 6 6 3 7 1?
如果函数内部声明的整数数组未初始化,则其值不确定。 如果在全局范围内所有函数之外声明类似的数组,则默认情况下它将用零初始化。
要使数组始终初始化为零,请执行以下操作:
int array_unsorted[MAX] = {0};
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为在 C 中,= {0}将用零初始化所有值。如果您说= {10, 20}它将按照写入方式初始化前两个元素,并将其余元素初始化为零。