如何用多行 C/C++ 編寫長字串?


長字串可以透過使用兩個雙引號( “ “ )在中間的任意位置中斷字串來寫入多行。

以下是用 C 展示此功能的程式。

示例

 實際演示

#include <stdio.h>
int main() {
   char *str = "This is the method "
               "to write long strings "
               "in multiple lines in C";
   puts(str);
   return 0;
}

輸出

以上程式的輸出如下。

This is the method to write long strings in multiple lines in C

現在讓我們來了解一下上面的程式。

可以透過使用兩個雙引號( “ “ )來中斷字串在中間的任意位置,從而將字串 str 寫入多行。然後使用 puts 顯示字串。以下程式碼段展示了這一過程。

char *str = "This is the method "
"to write long strings "
"in multiple lines in C";
puts(str);

更新於: 26-6 月-2020

13K+ 瀏覽量

開啟你的 職業生涯

完成課程獲得認證

開始
廣告