'\n'
is a char literal. "\n"
is a string literal (an array of chars, basically).
The difference doesn't matter if you're writing it to an ordinary stream. std::cout << "\n";
has the same effect as std::cout << '\n';
.
In almost every other context, the difference matters. A char is not in general interchangeable with an array of chars or with a string.
So for example std::string
has a constructor which takes a const char*
, but it doesn't have a constructor which takes a char
. You can write std::string("\n");
, but std::string('\n');
doesn't compile.
std::string
also has a constructor which takes a char
and the number of times to duplicate it. It doesn't have one that takes a const char*
and the number of times to duplicate it. So you can write std::string(5,'\n')
and get a string consisting of 5 newline characters in a row. You can't write std::string(5, "\n");
.
Any function or operation that you use will tell you whether it's defined for a char, for a C-style string, for both via overloading, or for neither.