Enumeration: An enumeration type is a user defined data type, which can take values only from a user defined list or named integer constants called enumerators.
Syntax:
enum tag
{
member1;
member2;
…………;
………...
} ;
Here enum is a keyboard, tag is an identifier that specifies the
name of the new enumeration type of being defined, member1, member2 are identifiers
which represent integer constant are
called enumerator constant or enumerator. The list of these enumerators is
called as enumerator list.
After the definition, we can declare variable of these new data type as -
enum tag var1, var2, var3;
Here var1, var2, var3 are variables of type enum tag. This variable can take value from the enumerator list.
The variable can also be declared with the definition as-
enum tag
{
member1;
member2;
…………;
………...
} var1, var2, var3;
Example:
enum month {Jan, Feb, Mar, Apr, May, Jun};
Here a new data type month is define and the enumerator list
contains six enumerators. internally the compiler treat the numerator as
integer constant. these are automatically assign integer values beginning
from 0,1,2..
Jan 0
Feb 1
Mar 2
Apr 3
.
Desired for values assigned to enumerator. it is also possible to explicitly assign any value to enumerator.
enum month {Jan, Feb=4, Mar, Apr, May=11, Jun};
Jan 0
Feb 4
Mar 5
Apr 6
May 11
Jun 12
We can assign any signed integer value to the enumerator.
enum boolean{true, false};
enum switch{off, on};
enum subject{Phy, Chem, Math, Cs, Ele}
No comments:
Post a Comment