Correct way to declare typedef enum in C?

Viewed 204

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;

?

2 Answers

Both definitions are valid.

If an enum member doesn't have an explicit value set for it, its value is that of the prior member plus 1. If the first member doesn't have an explicit value, its value is 0.

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.

Related