How do I assign the value of '\' to a character variable?
C++ doesn't recognize the backslash as a character and thinks there should be a 't' or 'n' or something afterwards.
How can the \
be assigned to a char
variable?
You need to escape it properly: '\\'
The backslash character is used as 'escape' sign for characters like '\"'
or '\''
to give them a special meaning for their appearance in literals instead of closing the literal.
There's also a number of escaped character values with more special meanings like e.g. '\n'
, that expands to a new line in output.
To give the special meaning for the \
character, it must be escaped itself:
char c = '\\';
// ^