📜  如何在多行C / C++中编写长字符串?

📅  最后修改于: 2021-05-30 18:19:07             🧑  作者: Mango

在一个我们想在C或C++中使用或打印很长的字符串的情况下,如何实现此目的?

在C / C++中,我们可以在中间的任何一点使用中间的两个双引号将字符串断开。下面是一个简单的示例来演示相同的内容。

#include
int main()
{
   // We can put two double quotes anywhere in a string
   char *str1  = "geeks""quiz"; 
  
   // We can put space line break between two double quotes
   char *str2  = "Qeeks"     "Quiz";
   char *str3  = "Qeeks"     
                 "Quiz";
  
   puts(str1);
   puts(str2);
   puts(str3);
  
   puts("Geeks"        // Breaking string in multiple lines
        "forGeeks");
   return 0;
}

输出:
怪胎
QeeksQuiz
QeeksQuiz
极客

以下是一些示例,这些示例使用两个双引号将长而长的字符串打断,以提高可读性。

#include
int main()
{
   char *str = "These are reserved words in C language are int, float, "
               "if, else, for, while etc. An Identifier is a sequence of"
               "letters and digits, but must start with a letter. "
               "Underscore ( _ ) is treated as a letter. Identifiers are "
               "case sensitive. Identifiers are used to name variables,"
               "functions etc.";
   puts(str);
   return 0; 
} 

输出:这些C语言保留字是int,float,if,else,for和while等。标识符是字母和数字的序列,但必须以字母开头。下划线(_)被视为字母。标识符区分大小写。标识符用于命名变量,函数等。

同样,我们可以在printf和or cout中编写长字符串。

#include
int main()
{
   char *str = "An Identifier is a sequence of"
               "letters and digits, but must start with a letter. "
               "Underscore ( _ ) is treated as a letter. Identifiers are "
               "case sensitive. Identifiers are used to name variables,"
               "functions etc.";
   printf ("These are reserved words in C language are int, float, "
            "if, else, for, while etc. %s ", str);
   return 0; 
}

输出:这些C语言保留字是int,float,if,else,for和while等。标识符是字母和数字的序列,但必须以字母开头。下划线(_)被视为字母。标识符区分大小写。标识符用于命名变量,函数等。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”