Are default enum values in C the same for all compilers?

Viewed 74182

When declaring an enum as shown below, do all C compilers set the default values as x=0, y=1, and z=2 on both Linux and Windows systems?

typedef enum {
    x,
    y,
    z
} someName;
4 Answers

If the first value of the enum variable is not initialized then the C compiler automatically assigns the value 0.The compiler keeps on increasing the value of preceeding enum variable by 1.

Eg:

enum months{jan,feb,mar}

Explanation: Value of jan will be 0,feb will be 1,mar will be 2.

enum months{jan=123,feb=999,mar}

Explanation: Value of jan will be 123,feb will be 999,mar will be 1000.

enum months{jan='a',feb='s',mar}

Explanation: Value of jan will be 'a',feb will be 's',mar will be 't'.

Related