Number of elements in an enum

后端 未结 8 2327
抹茶落季
抹茶落季 2020-12-05 02:06

In C, is there a nice way to track the number of elements in an enum? I\'ve seen

enum blah {
    FIRST,
    SECOND,
    THIRD,
    LAST
};

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 02:33

    I don't believe there is. But what would you do with such a number if they are not sequential, and you don't already have a list of them somewhere? And if they are sequential but start at a different number, you could always do:

    enum blah {
        FIRST = 128,
        SECOND,
        THIRD,
        END
    };
    const int blah_count = END - FIRST;
    

提交回复
热议问题