首页 > 解决方案 > 为什么我缺少终止“字符?

问题描述

//example1
#include<stdio.h>

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

}

//example2
#include<stdio.h>

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

}

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

标签: clinuxgcccompiler-errors

解决方案


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");

推荐阅读