为什么我会丢失终止字符?

时间:2019-06-22 04:01:41

标签: c linux gcc compiler-errors

//example1
#include<stdio.h>

int main()
{
  printf("hello World"
  );

}

//example2
#include<stdio.h>

int main()
{
  printf("hello World
  ");

}

在示例1中,编译器未显示任何错误,但是在示例2中,编译器显示了missing terminating " character错误。为什么?

1 个答案:

答案 0 :(得分:7)

C字符串文字不能包含文字换行符。这是C18标准,突出了相关部分。

  

6.4.5字符串文字

     

语法

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,例如"hello\nworld"

如果您希望将字符串文字分解为多行,请使用多个字符串文字:

printf("hello "
       "world");
相关问题