c - 如何在数组中存储00?

标签 c printf srand

我在数组中存储了四个数字,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/

相关文章:

c - 流打印和重定向

c - 如何解决 printf 中的 %d 输出显示 num1、num2 和 sum 的 ascii 值,而不是它们分配的值

c - 对 `pow' 和 `floor' 的 undefined reference

php通过socket编程传递c结构数据

c - 从一个位置打印到另一个位置 - 使用 printf

C:为什么 LLVM 从左到右评估 printf 而 GCC 从右到左评估?

c - 如何使用srand和rand不断产生新的数字?

c - 如何从 srand(time(0)) 知道 secret 数字

c - srand/rand 缓慢变化的起始值

无法找出为什么 valgrind 在我的具有结构数组的代码中给出错误