【发布时间】:2015-05-23 12:31:09
【问题描述】:
我创建了一个带有随机数的数组,但是当我打印数组时它们都是 0,这里有什么问题?我知道随机数在那里,但仍然是零?
int superArray[SIZE];
int index, counter;
srand( time(0) );
for (index = 0; index < SIZE; index++) //random numbers
superArray[index] = rand() % 21 +1;
for (index = 0; index < SIZE; index++) //vertical print
{
printf("%8.f\n", superArray[index]);
}
printf("%8d\n\n", superArray[8]); //verify that array has random numbers
【问题讨论】:
-
并且
printf("%8d\n\n", superArray[8]);不会水平打印数组。请改用for (index = 0; index < SIZE; index++){ printf("%d ", superArray[index]); } -
你的程序有未定义的行为;阅读
printf的手册。 -
如果你在编译时启用了警告(对于 gcc,至少 '-Wall -Wextra -pedantic' 那么你的编译器会标记:1)第一个 printf() 语句为格式修饰符与参数类型不匹配。 2)time()的参数是指针类型,所以0不正确,使用NULL。顺便说一句:询问运行时问题时,请发布可编译的代码。发布的代码没有。它缺少#include 语句和包含代码的函数。
标签: c arrays random printf format-specifiers