HomeC/C++ String Escape

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 SequenceASCII CodeDescription
\\0x5CBackslash
\'0x27Single quote
\"0x22Double quote
\n0x0ALine Feed (LF)
\r0x0DCarriage Return (CR)
\t0x09Horizontal Tab
\b0x08Backspace
\f0x0CForm Feed
\v0x0BVertical Tab
\a0x07Alert/Bell
\00x00NULL character
\oooOctalOctal value (1-3 digits)
\xhhHexHexadecimal 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.

Data is processed locally in your browser by default and will not be uploaded to any server. Upload will be clearly indicated if required.

© 2026 See-Tool. All rights reserved. | Contact Us