How to write a multi-line string literal in C++?
Hello everyone! I'm a beginner C++ developer, and I have a problem with long strings. I need to store a large block of text in code (for example, an SQL query, JSON, or just a message for output). Writing everything in one line is inconvenient — the code becomes unreadable and hard to format.
I tried splitting the string like this:
```cpp
std::string query = "SELECT * FROM users\nWHERE age > 18\nORDER BY name;";
```
But the compiler gives an error: "invalid string literal". I realized that this is not allowed. Then I tried concatenation:
```cpp
std::string query = "SELECT * FROM users" +
"WHERE age > 18" +
"ORDER BY name;";
```
This compiled, but the string ended up merged — spaces and line breaks were lost. I had to add '\n' manually, which is also inconvenient.
I heard that in C++11 there is
No answers yet.
Log in to leave an answer