【问题标题】:Why I am getting missing terminating " character?为什么我缺少终止“字符?
【发布时间】:2019-06-22 04:01:41
【问题描述】:
//example1
#include<stdio.h>

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

}

//example2
#include<stdio.h>

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

}

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

【问题讨论】:

  • exapmle2 中,编译器无法在同一行中找到结尾 ",而在 example1 中却可以。
  • 你可能想使用printf("hello World" "second line");

标签: c linux gcc compiler-errors


【解决方案1】:

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

【讨论】:

  • 先生在您的示例中,如果在第一行没有找到分号,编译器将如何处理语句?
  • 您几乎可以在标记之间的任何位置使用换行符,但不能在字符串文字内部。编译器将继续读取行,直到找到 ;
猜你喜欢
  • 1970-01-01
  • 2021-10-16
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多