121

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;
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
SSS
  • 2,344
  • 8
  • 22
  • 27
  • 3
    Yes, it is required by the standards, and I am sure someone will be able to cite them. – Nemo Jun 22 '11 at 02:05

4 Answers4

130

Yes. Unless you specify otherwise in the definition of the enumeration, the initial enumerator always has the value zero and the value of each subsequent enumerator is one greater than the previous enumerator.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 15
    and, this identical behavior is required by both C and C++. In C++, it's `[dcl.enum]`: "If the first enumerator has no initializer, the value of the corresponding constant is zero. An enumerator-definition without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one." – Ben Voigt Jun 22 '11 at 02:06
  • 3
    Yes, and also other languages that start with the letter C, like C#. – James McNellis Jun 22 '11 at 02:07
75

C99 Standard

The N1265 C99 draft says at 6.7.2.2/3 "Enumeration specifiers"

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.)

So the following always holds on conforming implementations:

main.c

#include <assert.h>
#include <limits.h>

enum E {
    E0,
    E1,
    E2 = 3,
    E3 = 3,
    E4,
    E5 = INT_MAX,
#if 0
    /* error: overflow in enumeration values */
    E6,
#endif
};

int main(void) {
    /* If unspecified, the first is 0. */
    assert(E0 == 0);
    assert(E1 == 1);
    /* Repeated number, no problem. */
    assert(E2 == 3);
    assert(E3 == 3);
    /* Continue from the last one. */
    assert(E4 == 4);
    assert(E5 == INT_MAX);
    return 0;
}

Compile and run:

gcc -std=c99 -Wall -Wextra -pedantic -o main.out main.c
./main.out

Tested in Ubuntu 16.04, GCC 6.4.0.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
8

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'.

Vagish
  • 2,520
  • 19
  • 32
Vignesh vicky
  • 81
  • 1
  • 1
  • 3
    mar being `'t'` is not guaranteed, there can be character sets in which the letters are not in consecutive alphabetical order – M.M May 27 '19 at 05:25
-18

Yes,enum value bydefult start from 0 to n'th element to any platform.

  • 17
    Consider how your answer adds to the pool of answers. That is, how does your new answer (5 years later) add something new that isn't covered in another answer? At first glance, it seems to be less informative than the other two answers. – LawfulEvil Jun 02 '16 at 11:45
  • 3
    Ehhh @LawfulEvil relax. Multiple answers give people looking at this in the future multiple perspectives. That said, this is a poorly-formatted, uninformative answer, but ample answers in and of themselves are not bad. – Kenny Worden Mar 01 '18 at 06:27