C/C++ String Escape
Escape special characters in C/C++ strings, including octal and hexadecimal sequences
About C/C++ String Escaping
C and C++ languages use backslash as the escape character prefix. Special characters in string literals must be represented using escape sequences, otherwise they cause compilation errors or unexpected behavior.
C/C++ supports various escape sequences, including simple escapes (like \n), octal escapes (like \012), and hexadecimal escapes (like \x0A). Understanding these escape sequences is crucial for correctly handling binary data and special characters.
Escape Sequence Categories
Simple Escape Sequences
Use backslash followed by a single character to represent common control characters
Octal Escape Sequences
Use \nnn format (1-3 octal digits) to represent any byte value
Hexadecimal Escape Sequences
Use \xnn format to represent any byte value, commonly used for binary data
Unicode Escapes (C++11)
Use \uXXXX or \UXXXXXXXX to represent Unicode characters
Escape Rules Reference
| Escape Sequence | ASCII Code | Description |
|---|---|---|
| \\ | 0x5C | Backslash |
| \' | 0x27 | Single quote |
| \" | 0x22 | Double quote |
| \n | 0x0A | Line Feed (LF) |
| \r | 0x0D | Carriage Return (CR) |
| \t | 0x09 | Horizontal Tab |
| \b | 0x08 | Backspace |
| \f | 0x0C | Form Feed |
| \v | 0x0B | Vertical Tab |
| \a | 0x07 | Alert/Bell |
| \0 | 0x00 | NULL character |
| \ooo | Octal | Octal value (1-3 digits) |
| \xhh | Hex | Hexadecimal value (2 digits) |
Common Use Cases
Embedded Systems
Handle serial communication and control characters in embedded C code.
Binary Protocols
Build network protocol packets containing specific byte sequences.
File Paths
Properly handle backslashes in Windows paths.
Formatted Output
Use newlines and tabs in printf format strings.
Usage Tips
- Octal escapes use at most 3 digits, values over 377 (255) produce warnings
- Hexadecimal escapes have no digit limit, but only the last two digits are valid
- C++11 introduced raw string literals R"(...)" to avoid escaping
- Wide strings (L"...") and UTF-8 strings (u8"...") use the same escape rules
- When concatenating string literals, escape sequences don't span across literal boundaries
Raw Strings (C++11)
C++11 introduced raw string literals that allow any character to be included in strings without escaping. The syntax is R"delimiter(content)delimiter", where delimiter is an optional user-defined delimiter.