When to use enums?

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

    Enums are primarily used because they are easier to read than integer numbers to programmers. For example, you can create an enum that represents dayy in a week.

    enum DAY {Monday, Tuesday....}
    

    Monday is equivalent to integer 0, Tuesday = Monday + 1 etc.

    So instead of integers thta represent each week day, you can use a new type DAY to represent this same concept. It is easier to read for the programmer, but it means the same to the compiler.

提交回复
热议问题