Exercise 1-2. Experiment to find out what happens when prints's argument string contains \c, where c is some character not listed above.
让读者自己探索用 printf() 输出 \c 后会出现什么。笔者用的代码如下:
#include <stdio.h>
int main(void) {
printf("\chello, world\n");
return 0;
}
由于 \c 不是一个转义字符,所以编译的时候系统会给warning(注意不是报错——error):
Exercise1_2.c: In function ‘main’:
Exercise1_2.c:4:30: warning: unknown escape sequence: '\c'
4 | printf("\chello, world\n");
|
可执行程序仍然会生成,执行结果如下:
> ./Exercise1_2
chello, world
可见,由于 \c 不是转义字符,所以 \ 会被忽略,而 c 会被第一个输出。
本文通过实例展示了当在printf()函数中使用未定义的转义序列`c`时,编译器会发出警告但依然能生成可执行程序。执行结果显示``被忽略,`c`直接作为普通字符输出。这说明在C语言中,不识别的转义序列将按原样输出。
163

被折叠的 条评论
为什么被折叠?



