HOWTO · C++
How to Declare Multiline String in C++
This article demonstrates how to declare a multiline string in C++.
On this page
Multiline strings matter when you work with lengthy text or want to preserve formatting in your source. C++ has no dedicated multiline-string type, but it offers several ways to build one: adjacent string literals, raw string literals, backslash-newline splicing, and preprocessor stringification. The techniques differ mainly in whether line breaks in the source become newlines in the value.
This guide walks through each approach with runnable examples and closes with a comparison so you can pick the right one for your code.
Build a multiline string with adjacent string literals
When the compiler sees two or more double-quoted string literals sitting next to each other, separated only by whitespace, it concatenates them into a single value at compile time. Splitting a literal across source lines is purely a formatting choice — no newline is added to the value unless you write \n yourself, and spaces must be included explicitly where words should stay separated.
Run this example:
#include <iostream>
#include <string>
int main() {
std::string message =
"I love"
" getting my doubts solved"
" by the help of Delft Stack Tutorials.";
std::cout << message << std::endl;
return 0;
}
I love getting my doubts solved by the help of Delft Stack Tutorials.
Adjacent literal concatenation works the same way when the destination is a const char * — a read-only pointer to the literal — rather than a std::string object:
#include <cstdio>
int main() {
const char *lines =
"Line 1 "
"Line 2 "
"Line 3";
std::printf("%s\n", lines);
return 0;
}
Line 1 Line 2 Line 3
Choose std::string when the text will be modified or passed to string-aware APIs; a const char * suits static text that is only printed or inspected.
Preserve line breaks with raw string literals
C++11 added raw string literals, which let physical line breaks live inside the literal itself. Everything between R"( and )" — including newlines, indentation, quotes, and backslashes — becomes part of the value verbatim, so no escape sequences are needed:
#include <iostream>
#include <string>
int main() {
std::string page = R"(I love
getting my doubts solved
by the help of Delft Stack Tutorials.)";
std::cout << page << std::endl;
return 0;
}
I love
getting my doubts solved
by the help of Delft Stack Tutorials.
Because the content is copied verbatim, watch two details: a newline typed right after the opening R"( is stored in the value, and indentation before continuation lines is preserved too. If your text ever contains the )" sequence, choose a custom delimiter such as R"tag(...)tag" — the closing marker then requires the tag, so a plain )" inside the text stays harmless.
Continue a string literal with backslash-newline splicing
A backslash as the very last character of a source line splices that line into the next before compilation. Inside a string literal, this lets the literal run across source lines, but the spliced line break is removed — it does not produce a newline in the value. That is the key difference from the \n escape, which does add a newline:
#include <cstdio>
int main() {
const char *text =
"First line\n"
"Second line\n\
Third line\n";
std::printf("%s", text);
return 0;
}
First line
Second line
Third line
The output shows both mechanisms at work: each \n writes a newline, while the backslash splice only joins the source lines — and the leading spaces on the continued line become real characters in the value. That whitespace sensitivity is the main hazard: indentation typed for readability ends up in the string, and any character after the backslash, even a space, breaks the splice. Use splicing only where you fully control the surrounding layout.
Understand macro stringification’s limitations
A variadic macro can turn its arguments into a string literal with the stringizing operator, as in #define MULTILINE_STRING(...) #__VA_ARGS__. Passing text spread over several lines yields one quoted literal — but the preprocessor normalizes every run of whitespace, including newlines, down to a single space:
#include <iostream>
#include <string>
#define MULTILINE_STRING(...) #__VA_ARGS__
int main() {
std::string message = MULTILINE_STRING(
I love getting my doubts solved by the help of Delft Stack Tutorials.);
std::cout << message << std::endl;
return 0;
}
I love getting my doubts solved by the help of Delft Stack Tutorials.
This makes macro stringification a constrained option, not a general-purpose multiline-string tool. It cannot preserve line breaks, the argument text is tokenized rather than treated as string content (so stray punctuation can produce surprising results), and macro-based code is harder to read and debug. Reach for it only when preprocessor-driven code genuinely needs to turn tokens into text.
Choose the right multiline string technique
- Raw string literals are the default choice when line breaks, indentation, or characters such as
\and"should survive verbatim into the value — ideal for embedded text blocks, JSON, SQL, or regular expressions. - Adjacent string literals give precise control over the value while keeping source lines short; every space and
\nis explicit, so nothing unintended slips in. - Backslash-newline splicing is fragile and indentation-sensitive; it mainly survives in legacy code and in macro definitions, where it is still required to continue a definition across lines.
- Macro stringification fits only preprocessor-heavy code that genuinely needs token-to-text conversion.
For a value that truly spans multiple lines, use a raw string literal. For a long single-line value formatted across your source, adjacent literals are clearest — and they pair naturally with either std::string or a const char *.