gpt4 book ai didi

c - 打印 0 初始化的数组元素的值什么也打印不出,为什么?

转载 作者:行者123 更新时间:2023-12-02 05:36:20 24 4
gpt4 key购买 nike

我必须将一个 char 数组初始化为 0。我是这样做的

char array[256] = {0};

我想检查它是否有效,所以我尝试测试它

#include <stdio.h>

int main()
{
char s[256] = {0};
printf("%c\n", s[10]);
return 0;
}

编译运行后,命令行输出什么也没有。

我错过了什么?也许我以错误的方式初始化了数组?

最佳答案

TL;DR -- %c 是字符表示。使用 %d 查看十进制 0 值。

相关,来自 C11,第 §7.21.6.1 章,(强调我的)

c          If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written.

仅供引用,请参阅 printable 的列表值(value)观。

也就是说,对于托管环境,int main() 应该是 int main(void),至少要符合标准。

关于c - 打印 0 初始化的数组元素的值什么也打印不出,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40575829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com