Number of elements in an enum

后端 未结 8 2317
抹茶落季
抹茶落季 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:28

    Well, since enums can't change at run-time, the best thing you can do is:

    enum blah {
        FIRST = 7,
        SECOND = 15,
        THIRD = 9,
        LAST = 12
    };
    #define blahcount 4 /* counted manually, keep these in sync */
    

    But I find it difficult to envisage a situation where that information would come in handy. What exactly are you trying to do?

提交回复
热议问题