DevAcademia lesson/C++ Basics
Special Characters
Topic progressTopic 30 of 109 - 28% complete
~6 min lesson19 learning blocks4 code examples1 checks/interactions
Lesson outline
Escape Sequences
In C++, special characters that cannot be typed directly or would otherwise be ambiguous are represented with escape sequences beginning with a backslash (\):
| Escape Sequence | Description |
|---|---|
| \n | New line (line feed) |
| \r | Carriage return |
| \t | Horizontal tab |
| \\ | Backslash |
| \" | Double quote |
| \' | Single quote |
| \a | Alert (bell) |
| \b | Backspace |
| \f | Form feed |
| \v | Vertical tab |
| \0 | Null character (NUL); in C-style strings it marks the end, but std::string can contain it |
| \xNN | Hex byte/char (e.g., \x41 == 'A') |
| \uNNNN / \UNNNNNNNN | Unicode code point (UTF-16/UTF-32 escape) |
Examples
Raw String Literals
C++11 introduced raw string literals, which treat backslashes and quotes literally without needing escape sequences. They are especially useful for file paths, JSON, or regular expressions.
Raw Strings with Custom Delimiters
If your content contains the sequence )", choose a custom delimiter so the compiler knows where the raw string ends.
Unicode & Numeric Escapes
Use hex or Unicode escapes when you need characters not easily typed on your keyboard. Combine with UTF-8 literals when appropriate.
Pitfalls
- In ordinary string literals, backslashes must be doubled for Windows paths (use raw strings to avoid this).
\0is a character; insidestd::stringit does not terminate the string automatically.- Escape processing does not occur inside raw string literals.
- Prefer
'\n'for a newline without flushing;std::endladds a newline and flushes.
Was this topic clear?