When to use enums?

后端 未结 5 1165
长发绾君心
长发绾君心 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:10

    Sometime you want to express something that is finite and discrete. An example from the GNU C Programming tutorial are compass directions.

    enum compass_direction
    {
      north,
      east,
      south,
      west
    };
    

    Another example, where the ability of enums to correspond to integers comes in handy, could be status codes. Usually you start the OK code with 0, so it can be used in if constructs.

提交回复
热议问题