【发布时间】:2022-01-20 12:30:00
【问题描述】:
所以我的代码是:
#include <stdio.h>
int main() {
char ch[5] = "funny";
printf("gum: ");
printf("ze numbre is %c \n", ch);
}
据我所知,它应该打印:
gum: ze numbre is funny
但不是输出ch 变量,而是输出一些奇怪的符号(它看起来像一个带有方形FF 的小红方块,有时上面写着F5),有什么提示吗?我正在用 VSCode 编码
【问题讨论】:
-
%s,而不是%c。但是[5]不足以容纳"funny",因为您需要为空终止符留出空间。 -
ch是指向char的指针 -
更高的警告级别可能有助于检测到这一点。我得到,
warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat=]在这段代码上。 -
char ch[5] = "funny";tochar ch[] = "funny";获取编译器计算数组长度 -
好的,我搞定了,谢谢 Fred 和 Ed,保持滴水不漏,祝你玩得开心
标签: c string compiler-errors printf undefined-behavior