What are the rules for the escape character \ in string literals? Is there a list of all the characters that are escaped?
In particular, when I use \ in a string literal in gedit, and follow it by any three numbers, it colors them differently.
I was trying to create a std::string constructed from a literal with the character 0 followed by the null character (\0), followed by the character 0. However, the syntax highlighting alerted me that maybe this would create something like the character 0 followed by the null character (\00, aka \0), which is to say, only two characters.
For the solution to just this one problem, is this the best way to do it:
std::string ("0\0" "0", 3) // String concatenation
And is there some reference for what the escape character does in string literals in general? What is ‘\a’, for instance?
Control characters:
(Hex codes assume an ASCII-compatible character encoding.)
\a=\x07= alert (bell)\b=\x08= backspace\t=\x09= horizonal tab\n=\x0A= newline (or line feed)\v=\x0B= vertical tab\f=\x0C= form feed\r=\x0D= carriage return\e=\x1B= escape (non-standard GCC extension)Punctuation characters:
\"= quotation mark (backslash not required for'"')\'= apostrophe (backslash not required for"'")\?= question mark (used to avoid trigraphs)\\= backslashNumeric character references:
\+ up to 3 octal digits\x+ any number of hex digits\u+ 4 hex digits (Unicode BMP, new in C++11)\U+ 8 hex digits (Unicode astral planes, new in C++11)\0=\00=\000= octal ecape for null characterIf you do want an actual digit character after a
\0, then yes, I recommend string concatenation. Note that the whitespace between the parts of the literal is optional, so you can write"\0""0".