Main Content

MISRA C++:2023 Rule 5.13.1

Within character literals and non raw-string literals, \ shall only be used to form a defined escape sequence or universal character name

Since R2024b

Description

Rule Definition

Within character literals and non raw-string literals, \ shall only be used to form a defined escape sequence or universal character name.

Rationale

Escape sequences are certain special characters represented in string and character literals. They are written with a backslash (\) followed by a character.

The C++ standards ISO/IEC 14882:2003 and later define a list of escape sequences. See Escape Sequences. Use of escape sequences (backslash followed by character) outside that list leads to undefined behavior.

Polyspace Implementation

The rule checker reports violations on use of escape sequences in contexts outside the following:

  • Escape sequences that are defined in ISO/IEC 14882:2003 and later standards.

  • Universal character names starting with \u or \U.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

void func () {
  const char a[2] = "\k"; //Noncompliant
  const char b[2] = "\b"; //Compliant
}

In this example, \k is not a recognized escape sequence.

Check Information

Group: Lexical Conventions
Category: Required

Version History

Introduced in R2024b