Unfortunatley, @Binyamin is right, you cannot do this with a listing. For this reason, I usually do the following in my projects:
// in .h typedef int MyEnum; struct { MyEnum value1; MyEnum value2; MyEnum value3; } MyEnumValues; // in .m __attribute__((constructor)) static void initMyEnum() { MyEnumValues.value1 = 10; MyEnumValues.value2 = 75; MyEnumValues.value3 = 46; }
This also has the advantage of being able to iterate over values, which is not possible with a normal enumeration:
int count = sizeof(MyEnumValues) / sizeof(MyEnum); MyEnum *values = (MyEnum *) &MyEnumValues; for (int i = 0; i < count; i++) { printf("Value %i is: %i\n", i, values[i]); }
All in all, this is my preferred way to do enumerations in C.
source share