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

SR:  8.4#1Checked automatically with code checker

Synopsis:Only the first member of an enum may be explicitly initialised, if any, unless all items are explicitly initialised.
Language:C
Severity Level:4
Category:DECLARATIONS


Description:

If an enumerator list is given with no explicit initialisation of members, then C allocates a sequence starting at 0 for the first element and increasing by 1 for each subsequent element.

An explicit initialisation of the first element is permitted by the above rule, forces the allocation of integers to start at the given value. When adopting this approach it is essential to ensure that the initialisation value used is small enough that no subsequent value in the list will exceed the int storage used by the enumeration constants.

Explicit initialisation of all items in the list, which is also permissible, prevents the mixing of automatic and manual allocation, which is error prone. However it is then the responsibility of the programmer to ensure that all values are in the required range, and that values are not unintentionally duplicated.

Example:

enum colour { red=3, blue, green, yellow=5 }; /* non compliant */
/* green and yellow represent the same value - this is duplication */
...
enum colour { red=3, blue=4, green=5, yellow=5 }; /* compliant */
/* green and yellow represent the same value - this is duplication */