【发布时间】:2019-08-31 18:02:12
【问题描述】:
当我打印 8 个或更多字符时,符号总是在第 8 个字符之后打印。有谁知道代码有什么问题,我该如何解决?
我尝试过使用不同数量的字符,但总是在超过 8 或 8 个时发生。
#include <stdio.h>
int main() {
char ch = 0;
char temp[100];
int i = 0;
while (scanf("%c", &ch) == 1) {
if (ch != '\n') {
temp[i] = ch;
printf("%s", temp);
i++;
}
}
return 0;
}
我的预期结果是
1 12 123 123412345123456123456712345678
我的实际输出是
1 12 123 123412345123456123456712345678xxx
x 代表符号
【问题讨论】:
-
你不会空终止字符串。任何事情都可能发生,因为它会调用未定义的行为