What is the use of the enum keyword ? Where can we use enumerated data-type?
Can anyone explain with an example?
What is the use of the enum keyword ? Where can we use enumerated
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Enums are enumerators (ok, trivial) treated always as integers, and they are used to put in order something you could use later, like common values or indexes. But, as the name says, they are enumerated, always ascending, like:
enum myenum {January, February, March, April, May, June, July, August, September, October, November, December
};
Then, if you have an array, say:
You can call your months using:
Also, if you declare:
This your program will start the counter of the enumeration from 1 and so on.
Finally, about this last rule: you can set initial values as you want, e.g., 10, 20, 30, 42 etc., but you can also set one of the enumerated values and let others be sorted by default.