簡體   English   中英

為什么我會丟失終止字符?

[英]Why I am getting missing terminating " character?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM