View Discussion Improve Article Save Article View Discussion Improve Article Save Article Enumerated type (enumeration) is a user-defined data type which can be assigned some limited values. These values are defined by the programmer at the time of declaring the enumerated type. If we assign a float value in a character value, then the compiler generates an error. In the same way if we try to assign any other value to the enumerated data types, the compiler generates an error. Enumerator types of values are also known as enumerators. It is also assigned by zero the same as the array. It can also be used with switch statements. Syntax:
enum keyword is used to declare enumerated types after that enumerated type name was written then under curly brackets possible values are defined. After defining Enumerated type variables are created. It can be created in two types:-
By default, the starting code value of the first element of enum is 0 (as in the case of array) . But it can be changed explicitly. For example: enum enumerated-type-name{value1=1, value2, value3}; And, The consecutive values of the enum will have the next set of code value(s). For example: //first_enum is the enumerated-type-name enum first_enum{value1=1, value2=10, value3}; In this case, first_enum e; e=value3; cout<<e; Output: 11 Example 1: CPP
Example 2: CPP
Output: 0 1 2 3 4 5 6 7 8 9 10 11 |