我在数组中存储了四个数字,00,11,22,33
。当我生成随机数并打印它时,它显示 0
而不是 00
(当选择第一个元素时)。其他数字都很好并且显示正确。如何将 00 存储在数组中以便其正确显示?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int myArray[4] = { 00,11,22,33 };
int randomIndex = rand() % 4;
int randomIndex1 = rand() % 4;
int randomIndex2 = rand() % 4;
int randomIndex3 = rand() % 4;
int randomValue = myArray[randomIndex];
int randomValue1 = myArray[randomIndex1];
int randomValue2 = myArray[randomIndex2];
int randomValue3 = myArray[randomIndex3];
printf("= %d\n", randomValue);
printf("= %d\n", randomValue1);
printf("= %d\n", randomValue2);
printf("= %d\n", randomValue3);
return(0);
}
最佳答案
00
数字,与 0
数字完全相同,而 11
显然与 1< 是不同的数字
。
考虑存储字符串。或者,如果您想显示 00
,只需使用 %02d
作为格式字符串显示 2 个字符:
printf("= %02d\n", randomValue);
如果这确实是您的整个程序,您甚至可以只修改数组,然后打印值两次,例如:
int myArray[4] = {0,1,2,3};
. . .
printf("= %d%d\n", randomValue, randomValue);
关于c - 如何在数组中存储00?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41901950/