How to get enum count
I have a Enum
typedef enum{
DEL_TIME_VALUE = -1,
DEL_TIMESEC_VALUE = 100,
DEL_TIMEMIN_VALUE = 200,
DEL_TIMEHOUR_VALUE = 300,
DEL_DAY_VALUE = 1000,
DEL_COUNT_VALUE = 1000,
....
.....
.....
DEL_END =90002
}WORKINGTIME;
How do i get the enum count.
I try below for loop!
for(int i=DEL_TIME_VALUE; i<=DEL_END; i++) {
}
I guess its not good one!
can any one tell me how to get enum count! which are declared in enum.
Thanks in advance!
You can’t.
There is one technique that allows you to get the enum count. It looks like
Now the value
enum_countis the count of values in the enum. However, this technique only works if the enums all carry their implicit value, wherevalue_oneis0,value_twois1, etc. Because of this, the last value in the enum always has the value of the count of enum values. In your case, your enum constants have explicit values that are not monotonically incrementing. There is no way to derive a count from this type of enum. And even in the theoretical world where you could derive a count, that wouldn’t help you because you could not derive the value of a given enum constant.