How to write long strings in Multi-lines C/C++?

Image a situation where we want to use or print a long long string in C or C++, how to do this?

In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
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;
}

chevron_right


Output:
geeksquiz
QeeksQuiz
QeeksQuiz
GeeksforGeeks

Below are few examples with long long strings broken using two double quotes for better readability.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
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; 

chevron_right


Output: These are reserved words in C language are int, float, if, else, for, while etc. An Identifier is a sequence ofletters 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.



Similarly, we can write long strings in printf and or cout.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
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; 
}

chevron_right


Output: These are reserved words in C language are int, float, if, else, for, while etc. An Identifier is a sequence ofletters 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.

This article is contributed by Ayush Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Improved By : rahul1642, nidhi_biet

Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.