compiling with gcc C99
I have been using enums for a while now. However, I am using some sample code to develop my application. And I came across some code like this. I have been informed this is the best practice use when using enums. But I don’t see how this has any advantages.
typedef enum { TYPE_DATE, TYPE_TIME, TYPE_MONEY, TYPE_COUNT, TYPE_UNKNOWN = TYPE_COUNT } string_type_e;
Why have the TYPE_COUNT and why assign TYPE_COUNT to TYPE_UNKNOWN?
Many thanks for any suggestions,
By default, enums are automatically given integer values starting from 0 by the compiler. So date will be zero, time one and money two. The next value is given to the ‘psuedo’ enum value
TYPE_COUNT, which will get given the value three, which happens to be the number of ‘proper’ enum values.TYPE_UNKNOWNis another value which represents something which isn’t a ‘proper’ value, so will fail a teste < TYPE_COUNT. Having it equal toTYPE_COUNTmeans that each distinct meaningful value is contiguous, but I’m not aware of any significant advantage to that (there would be ifTYPE_COUNTwas one less than a power of 2, which might effect what representation the compiler could use, and its ‘nice’ to have the values contiguous, but it doesn’t really matter, as you wouldn’t increment them pastTYPE_COUNTanyway)