[previous] MISRA-C:2004  Rule  7.1:  (Required) [next] Octal constants (other than zero) and octal escape sequences shall not be used.
Any integer constant beginning with a '0' (zero) is treated as octal. So there is a danger, for example, with writing fixed length constants. For example, the following array initialisation for 3-digit bus messages would not do as expected (052 is octal, i.e. 42 decimal):

code[1] = 109;      /* equivalent to decimal 109 */
code[2] = 100;      /* equivalent to decimal 100 */
code[3] = 052;      /* equivalent to decimal 42 */
code[4] = 071;      /* equivalent to decimal 57 */

Octal escape sequences can be problematic because the inadvertent introduction of a decimal digit ends the octal escape and introduces another character. The value of the first expression in the following example is implementation-defined because the character constant consists of two characters, '\10' and '9'. The second character constant expression below contains the single character '\100'. Its value will be implementation-defined if character 64 is not represented in the basic execution character set.

code[5] = '\109';   /* implementation-defined, two character constant */
code[6] = '\100';   /* set to 64, or implementation-defined           */

It is better not to use octal constants or escape sequences at all, and to statically check for any occurrences. The integer constant zero (written as a single numeric digit), is strictly speaking an octal constant, but is a permitted exception to this rule. Additionally "\0" is the only permitted octal escape sequence.


QAC messages that encompass this guideline:

0336 Macro defined as an octal constant.
0339 Octal constant used.
3628 Octal escape sequences used in a character constant or string literal.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents