Search code examples
cenumstypedef

Correct way to declare typedef enum in C?


Is it a correct way to declare enum in C like so:

typedef enum
 {
     K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,        
     K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,        
     K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90, 
     K4ABT_SENSOR_ORIENTATION_FLIP180,            
 } k4abt_sensor_orientation_t;

Shouldn't it be:

typedef enum
 {
     K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,        
     K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,        
     K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2, 
     K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,            
 } k4abt_sensor_orientation_t;

?


Solution

  • From the C Standard (6.7.2.2 Enumeration specifiers)

    3 The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.127) An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.

    So for example all these declarations are equivalent

    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90, 
         K4ABT_SENSOR_ORIENTATION_FLIP180,            
     } k4abt_sensor_orientation_t;
    
    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90, 
         K4ABT_SENSOR_ORIENTATION_FLIP180,            
     } k4abt_sensor_orientation_t;
    
    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90, 
         K4ABT_SENSOR_ORIENTATION_FLIP180,            
     } k4abt_sensor_orientation_t;
    
    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2, 
         K4ABT_SENSOR_ORIENTATION_FLIP180,            
     } k4abt_sensor_orientation_t;
    
    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90, 
         K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,            
     } k4abt_sensor_orientation_t;
    
    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2, 
         K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,            
     } k4abt_sensor_orientation_t;
    
    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,            
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2, 
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,        
         K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,        
     } k4abt_sensor_orientation_t;
    

    However for more readability I would prefer this declaration

    typedef enum
     {
         K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,        
         K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,        
         K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2, 
         K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,            
     } k4abt_sensor_orientation_t;
    

    especially when an enumeration contains many enumerators.