DCL18-C. 10 進値を指定するとき、整数定数を 0 で始めない
C 言語標準は、8 進定数は 0 で始まる 8 進数字(0 1 2 3 4 5 6 7)として定義している。
8 進定数として 10 進数の値を誤って指定すると、プログラミングエラーが発生する可能性がある。
違反コード
以下のコード例では、10 進定数の先頭に誤ってゼロを埋め込んでいるため、すべての定数が固定長になっている。
i_array[0] = 2719; i_array[1] = 4435; i_array[2] = 0042;
i_array[2]
に 10 進数 42 が割り当てられるように見えるが、実際には 10 進数 34 が割り当てられる。
適合コード
間違った値が使用されるのを防止し、コードをより読みやすくするには、値が 10 進数を意味するのなら、定数の先頭にゼロを埋め込まないことだ。
i_array[0] = 2719; i_array[1] = 4435; i_array[2] = 42;
リスク評価
10 進数の値を 8 進数として誤って表記すると、比較と割り当てが正しく行われない恐れがある。
ルール |
深刻度 |
可能性 |
修正コスト |
優先度 |
レベル |
---|---|---|---|---|---|
DCL18-C |
低 |
低 |
低 |
P3 |
L3 |
関連するガイドライン
MISRA C:2012 | Rule 4.1 (required) |
翻訳元
これは以下のページを翻訳したものです。
DCL18-C. Do not begin integer constants with 0 when specifying a decimal value (revision 27)