//example1
#include<stdio.h>
int main()
{
printf("hello World"
);
}
//example2
#include<stdio.h>
int main()
{
printf("hello World
");
}
在示例 1 中,编译器没有显示任何错误,但在示例 2 中,它显示了一个missing terminating "character
错误。为什么?
最佳答案
C 字符串文字不能包含文字换行符。这是 C18突出显示相关部分的标准。
6.4.5 String literals
Syntax
string-literal: encoding-prefixopt " s-char-sequenceopt " s-char-sequence: s-char s-char-sequence s-char s-char: any member of the source character set except the double-quote ", backslash \, or new-line character <---- HERE escape-sequence
如果您希望您的字符串文字包含换行符,请改用 \n
,例如“你好\nworld”
。
如果您希望您的字符串文字被分成多行,请使用多个字符串文字:
printf("hello "
"world");
关于c - 为什么我缺少终止“字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712476/