You've two different answers to your question which provide many of the same functionalities.
Both
char *str="Line 1\nLine 2\nLine 3";
and
char str[]="Line 1\nLine 2\nLine 3";
Allow you to print a paragraph as such:
printf("%s",str);
However, the first declaration (char *str
) creates a string in what is generally read-only memory, whereas the second allows the string to be edited during run-time. This delineation is important, but not always clear. See this question for a few more details.
The character \n
is the line feed character, and you should check to ensure that it behaves the way you expect it to on your target platform. For instance, on DOS you may need to use `"\r\n", which is carriage return + line feed. Wiki has an article about this.
Another difference in these forms, as one commenter pointed out, is that *str
works as a pointer whereas str[]
does not. They often, but not always, have the same behavior; this question has more information regarding this.
As some commenters have pointed out, there is a limit on the length of string literals in some compilers. MSVC has a limit of 2048 characters (see here) whereas GCC has no limit, by some accounts. A length of at least 509 one-byte characters is guaranteed by C90; this was increased to 4095 in C99.
Regardless, if you want to avoid this length limit or you want to organize the text in a prettier way, you can use this format (note that newlines and quotes must be used explicitly, the compiler treats the adjacent strings as being concatenated):
char *str =
"Line 1\n"
"Line 2\n"
"Line 3\n";
or this (the backslashes at the end of the line escape the newline you've inserted for the formatting, if you indent your code here, that will become part of the string):
char *str =
"Line 1 \
Line 2 \
Line 3";