Main Content

MISRA C++:2008 Rule 2-13-2

Octal constants (other than zero) and octal escape sequences (other than "\0") shall not be used

Description

Rule Definition

Octal constants (other than zero) and octal escape sequences (other than "\0") shall not be used.

Rationale

Octal constants are denoted by a leading zero. A developer or code reviewer can mistake an octal constant as a decimal constant with a redundant leading zero.

Octal escape sequences beginning with \ can also cause confusion. Inadvertently introducing an 8 or 9 in the digit sequence after \ breaks the escape sequence and introduces a new digit. A developer or code reviewer can ignore this issue and continue to treat the escape sequence as one digit.

Polyspace Implementation

The rule checker reports violations on:

  • Constants beginning with a leading zero.

  • Escape sequences that begin with a \ followed by a zero.

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(void) {
  int busData[6];
  
  busData[0] = 100;
  busData[1] = 108;
  busData[2] = 052;      //Noncompliant
  busData[3] = 071;      //Noncompliant
  busData[4] = '\109';   //Noncompliant
  busData[5] = '\100';   //Noncompliant

}

The checker flags all octal constants (other than zero) and all octal escape sequences (other than \0).

In this example:

  • The octal escape sequence contains the digit 9, which is not an octal digit. This escape sequence has implementation-defined behavior.

  • The octal escape sequence \100 represents the number 64, but the rule checker forbids this use.

Check Information

Group: Lexical Conventions
Category: Required

Version History

Introduced in R2013b