When to use enums?

后端 未结 5 1144
长发绾君心
长发绾君心 2021-01-24 07:51

I\'m currently reading about enums in C. I understand how they work, but can\'t figure out situations where they could be useful. Can you give me some simple examples where the

5条回答
  •  灰色年华
    2021-01-24 08:30

    Enums are just a way to declare constant values with more maintainability. The benefits include:

    • Compilers can automatically assign values when they are opaque.
    • They mitigate programmers doing bad things like int monday = SUNDAY + 1.
    • They make it easy to declare all of your related constants in a single spot.

    Use them when you have a finite list of distinct, related values, as in the suits of a deck of cards. Avoid them when you have an effectively unbounded list or a list that could often change, as in the set of all car manufacturers.

提交回复
热议问题