TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
TIOBE - C Coding Standard
Search

SR:  7.19#1

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.

Example:

...
#if (1u-2u) /* wraps to maximum positive value */
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.:

Example:

...
#if (2000000000u+2000000000u+2000000000u)
...
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.:

Example:

...
#if (2000000000u * 3u) /* unrepresentable value */
...
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.:

Example:

...
int a[(unsigned char)3000];
...