Not to be confused with how to split a string parsing wise, e.g.:
Split a string in C++?
I am a bit confused as to how to split a string onto multiple lines in c++.<
Don't put anything between the strings. Part of the C++ lexing stage is to combine adjacent string literals (even over newlines and comments) into a single literal.
#include
#include
main() {
std::string my_val ="Hello world, this is an overly long string to have"
" on just one line";
std::cout << "My Val is : " << my_val << std::endl;
}
Note that if you want a newline in the literal, you will have to add that yourself:
#include
#include
main() {
std::string my_val ="This string gets displayed over\n"
"two lines when sent to cout.";
std::cout << "My Val is : " << my_val << std::endl;
}
If you are wanting to mix a #define
d integer constant into the literal, you'll have to use some macros:
#include
using namespace std;
#define TWO 2
#define XSTRINGIFY(s) #s
#define STRINGIFY(s) XSTRINGIFY(s)
int main(int argc, char* argv[])
{
std::cout << "abc" // Outputs "abc2DEF"
STRINGIFY(TWO)
"DEF" << endl;
std::cout << "abc" // Outputs "abcTWODEF"
XSTRINGIFY(TWO)
"DEF" << endl;
}
There's some weirdness in there due to the way the stringify processor operator works, so you need two levels of macro to get the actual value of TWO
to be made into a string literal.