2

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;

?

5
  • Do you need them to have particular values? If so, use the second form. This way if you reorder them or omit one it doesn't screw everything up numerically. Commented May 5, 2021 at 20:49
  • 1
    @blackmika The both declarations are equivalent. Commented May 5, 2021 at 20:49
  • <off-topic> I think FLIP180 is not the same as ROTATE... Commented May 5, 2021 at 20:54
  • Aside: I have found it useful to always end the list like with K4ABT_SENSOR_ORIENTATION_N as for later value checking with if (x >= 0 && x < K4ABT_SENSOR_ORIENTATION_N) GoodToGo(); Commented May 5, 2021 at 22:00
  • @VladFeinstein not in our case Commented May 5, 2021 at 23:56

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Another benefit of explicitly listing the enumerator values is to avoid silent problems when using source control and entries are added or removed
@Vlad from Moscow thanks for the comprehensive explanation!
@M.M explicity and avoiding silent problems matter!
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.