2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-26 17:29:13 +02:00
Integer literals starting with a zero are octal rather than decimal values. While using octal values is fully supported, most developers do not have experience with them. They may not recognize octal values as such, mistaking them instead for decimal values.
2020-06-30 12:47:33 +02:00
2021-04-26 17:29:13 +02:00
Hexadecimal literals (``++0xdeadbeef++``) and binary literals (``++0b0101'0110'00011++``, available since {cpp}14), on the other hand, have a clear marker (``++0x++`` or ``++0b++``) and can be used to define the binary representation of a value.
2022-02-17 13:09:22 +01:00
Character literals starting with ``\`` and followed by one to three digits are octal escaped literals.
Character literals starting with ``\x`` and followed by one or more hexits are hexadecimal escaped literals, and are usually more readable.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-26 17:29:13 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-26 17:29:13 +02:00
----
int myNumber = 010; // Noncompliant. myNumber will hold 8, not 10 - was this really expected?
2022-02-17 13:09:22 +01:00
char myChar = '\40'; // Noncompliant. myChar will hold 32 rather than 40
2021-04-26 17:29:13 +02:00
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-26 17:29:13 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-26 17:29:13 +02:00
----
int myNumber = 8; // Use decimal when representing the value 8
// or
int myNumber = 0b1000; // Use binary or hexadecimal for a bit mask
2022-02-17 13:09:22 +01:00
char myChar = '\x20'; // Use hexadecimal
// or
char myChar = '\n'; // Use the common notation if it exists for the literal
2021-04-26 17:29:13 +02:00
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-26 17:29:13 +02:00
2022-02-17 13:09:22 +01:00
* Octal values have traditionally been used for user permissions in Posix file systems, and this rule will ignore octal literals used in this context.
* ``'\0'`` is a common notation for a null character, so the rule ignores it.
2024-08-12 15:13:25 +02:00
* Since {cpp}23, an octal escape sequence can also be written `\o{123}`. Since this notation is explicit, the rule ignores it too. See S7040.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2020-06-30 12:47:33 +02:00
2024-08-12 15:13:25 +02:00
=== External coding guidelines
2024-08-06 18:05:05 +02:00
* MISRA {cpp}:2023, 5.13.3 - Octal constants shall not be used
2020-06-30 12:47:33 +02:00
* MISRA C:2012, 7.1 - Octal constants shall not be used
2024-08-06 18:05:05 +02:00
* MISRA {cpp}:2008, 2-13-2 - Octal constants (other than zero) and octal escape sequences (other than "\0") shall not be used
2020-12-21 15:38:52 +01:00
* https://wiki.sei.cmu.edu/confluence/x/atYxBQ[CERT, DCL18-C.] - Do not begin integer constants with 0 when specifying a decimal value
2021-06-02 20:44:38 +02:00
2024-08-12 15:13:25 +02:00
=== Related rules
2024-08-16 10:49:40 +02:00
* S7040 - Escape sequences should use the delimited form (\u{}, \o{}, \x{})
2024-08-12 15:13:25 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]