c - 为什么我缺少终止“字符?

标签 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 错误。为什么?

最佳答案

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/

相关文章:

c - 错误: ‘SIGKILL’ undeclared (first use in this function)

c - 将 "String"拆分为 C 中的字符

c - C中函数调用运算符的结合性

c - scandir() 导致段错误,即使有有效的绝对路径?

c - C 中的 System() 函数显示有错误的输出

c - 写入硬件 Controller 寄存器和 DMA 如何在 ARM arch 机器上工作?

c - 欧拉3 : Floating point exception core dumped

c++ - 安装g++并编译问题

c - GCC 内联汇编错误 : "Operand size mismatch for ' int'"

c - Linux调度。 (线程)