我试图在char数组中使用"回车"ASCII值,然后使用printf("%s"(打印字符串,但我得到了奇怪的结果。
这是我的代码:
#include <iostream>
int main(){
char text[10];
text[0] = '1';
text[1] = '2';
text[2] = 13;
text[3] = 'n';
text[4] = '3';
text[5] = 13 ;
text[6] = '4';
printf("%s", text);
}
输出为:43
但是当我在数组中添加一个字符时,如下所示:
#include <iostream>
int main(){
char text[10];
text[0] = '1';
text[1] = '2';
text[2] = 13;
text[3] = 'n';
text[4] = '3';
text[5] = 13;
text[6] = '4';
text[7] = '5';
printf("%s", text);
}
输出为45
然后我使用字符10
而不是像这样的13
:
#include <iostream>
int main(){
char text[10];
text[0] = '1';
text[1] = '2';
text[2] = 10 ;
text[3] = 'n';
text[4] = '3';
text[5] = 10 ;
text[6] = '4';
text[7] = '5';
printf("%s", text);
}
输出变为:
12
n3
45
有人能向我解释一下第一次输出和第二次输出之间差异的原因吗?
字符10
和字符13
在函数printf("%s")
处理它们的方式上有什么不同?
在ASCII中:
-
10代表
'n'
-
13代表
'r'
\n(换行(:
将活动位置移动到下一行的初始位置。
\r(回车(:
将活动位置移动到当前行的初始位置。
继续操作之前,请确保字符串以null结尾。添加