目前知道的字符串有以下几种表现形式:
// 字符串的几种表现形式
char str1[] = "abc";
char str2[100] = "abc";
char str3[] = {'a' , 'b' , 'c'};
char str4[100] = {'a' , 'b' , 'c'};
char str5[] = {"abc"};
char str6[100] = {"abc"};
输出他们的结果是:
其中第五种表现形式对应的字符串定义是:char str3[] = {'a' , 'b' , 'c'}
。输出的语句是:printf("第五种表示:%s\n" , str3)
为什么会出现这样的结果??
完整的运行结果(仅出错部分)
c的字符串数组需要在末尾有'0'标志字符串结束, 很多时候'0'会自动添加. 但是
char[] str = {'a', 'b', 'c'};
不会这样, 所以你需要char[] str = {'a', 'b', 'c', '\0}
;你漏了'0', 所以scanf没有读到'0', 会一直读下去, windows下会输出烫, 至于为什么, 读这里
https://www.zhihu.com/questio...
https://zhuanlan.zhihu.com/p/...
爆栈的相关帖子:
https://stackoverflow.com/que...
https://stackoverflow.com/que...
https://stackoverflow.com/que...