What is the use of enum keyword ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the use of enum keyword ?

I know what it is and how it works but what is the use of this keyword

21st Nov 2016, 9:53 AM
Chitransh Vishwakarma
Chitransh Vishwakarma - avatar
4 Answers
0
If you don't want to use a macro, but actually want to define an array of macros, enums are useful. The values of enums will be automatically defined (incremental values) , so you don't have to define a value for each enum, unlike macro. Also enums can be a type definition so you can actually try to restrict the value of a parameter inside a function.
21st Nov 2016, 4:21 PM
meceware
0
will you please give an example illustrating what you said. Thanks
21st Nov 2016, 4:25 PM
Chitransh Vishwakarma
Chitransh Vishwakarma - avatar
0
Like this typedef enum cardsuit { Clubs = 5, Diamonds, Hearts, Spades }; cardsuit myCards; I defined a type of an enum and used it as a variable. So you can use it in a for example if clause. if (myCards == Clubs) .... Also I started Clubs from 5, so next enums continue with the number 6 and increments accordingly. I didn't define it as a macro.
21st Nov 2016, 4:36 PM
meceware
0
thanks for your help buddy !
21st Nov 2016, 5:59 PM
Chitransh Vishwakarma
Chitransh Vishwakarma - avatar