String literals spanning multiple lines

String literals spanning multiple lines#

If you need a string variable which spans multiple lines:

#include <stdio.h>

// Multiline string with interpolation (Raw string literals)
auto table = 
"+---+---+---+---+\n"
"| %d |   | %d |   |\n"
"| %d |   | %d |   |\n"
"+---+---+---+---+\n"
;


int main() {
  printf(table, 1, 2, 3, 4);
}
+---+---+---+---+
| 1 |   | 2 |   |
| 3 |   | 4 |   |
+---+---+---+---+

You can also use the following GCC extension, which is not part of the C standard, so it may be not be supported in every compiler. It is borrowed from the C++.

#include <stdio.h>

// Multiline string with interpolation (Raw string literals)
auto table = R"(
+---+---+---+---+
| %d |   | %d |   |
| %d |   | %d |   |
+---+---+---+---+
)";


int main() {
  printf(table, 1, 2, 3, 4);
}
+---+---+---+---+
| 1 |   | 2 |   |
| 3 |   | 4 |   |
+---+---+---+---+