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?
String literal syntaxUse the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \' .
A character literal contains a sequence of characters or escape sequences enclosed in single quotation mark symbols, for example 'c' . A character literal may be prefixed with the letter L, for example L'c' .
In C, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a newline character.
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 character
If 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"
.
\0 will be interpreted as an octal escape sequence if it is followed by other digits, so \00 will be interpreted as a single character. (\0 is technically an octal escape sequence as well, at least in C).
The way you're doing it:
std::string ("0\0" "0", 3) // String concatenation
works because this version of the constructor takes a char array; if you try to just pass "0\0" "0" as a const char*, it will treat it as a C string and only copy everything up until the null character.
Here is a list of escape sequences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With