0% found this document useful (0 votes)
6 views

Enumerated Type Declaration: Enum Flag (Const1, Const2, ..., Constn)

An enumeration defines a set of named integral constants that can be used to represent a set of distinct values. The enum keyword is used to define an enumeration, which assigns increasing integer values to the named constants starting from 0 by default, but these can be explicitly set. Enumerations are commonly used to represent a set of possible states or values like the suits in a deck of cards, and variables declared as an enum type can only take on one of the defined constant values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Enumerated Type Declaration: Enum Flag (Const1, Const2, ..., Constn)

An enumeration defines a set of named integral constants that can be used to represent a set of distinct values. The enum keyword is used to define an enumeration, which assigns increasing integer values to the named constants starting from 0 by default, but these can be explicitly set. Enumerations are commonly used to represent a set of possible states or values like the suits in a deck of cards, and variables declared as an enum type can only take on one of the defined constant values.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

An enumeration consists of integral constants.

To define an enumeration,
keyword enum is used.

enum flag { const1, const2, ..., constN };

Here, name of the enumeration is flag.

And, const1, const2,...., constN are values of type flag.

By default, const1 is 0, const2 is 1 and so on. You can change default values of


enum elements during declaration (if necessary).

// Changing default values of enum

enum suit {

club = 0,

diamonds = 10,

hearts = 20,

spades = 3,

};

Enumerated Type Declaration


When you create an enumerated type, only blueprint for the variable is created.
Here's how you can create variables of enum type.

enum boolean { false, true };

enum boolean check;

Here, a variable check of type enum boolean is created.


Here is another way to declare same check variable using different syntax.

enum boolean

false, true

} check;

Why enums are used?


Enum variable takes only one value out of many possible values. Example to
demonstrate it,

#include <stdio.h>

enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;

int main()
{
card = club;
printf("Size of enum variable = %d bytes", sizeof(card));
return 0;
}

Output
Size of enum variable = 4 bytes

It's because the size of an integer is 4 bytes.

This makes enum a good choice to work with flags.

You can accomplish the same task using structures. However, working with enums
gives you efficiency along with flexibility.

You might also like