TIOBE - C Coding Standard

Synopsis: | Arithmetic operations on constant expressions must not lead to wrapround |

Language: | C |

Severity Level: | 3 |

Category: | EXPRESSIONS |

Description: |

Arithmetic operations on constant expressions must not lead to wrapround. Consider the following examples:
This constant expression involves unsigned arithmetic and generates a value too large to be represented. Modulo arithmetic will be performed to reduce the result to a value which is representable.
This constant expression involves unsigned addition and generates a value too large to be represented. Modulo arithmetic will be performed to reduce the result to a value which is representable. e.g.:... #if (1u-2u) /* wraps to maximum positive value */
This constant expression involves unsigned multiply and generates a value too large to be represented. Modulo arithmetic will be performed to reduce the result to a value which is representable. e.g.:... #if (2000000000u+2000000000u+2000000000u) ...
Here, the constant expression is being cast to an unsigned type which is not capable of representing its complete value. In these cases the value of the con-stant expression will become the modulo of its actual value and the maximum value representable by the resultant type. e.g.:... #if (2000000000u * 3u) /* unrepresentable value */ ...
... int a[(unsigned char)3000]; ... |