Seven important points of enumeration in c language

enum in c

An enum in c is user defined data type and it consists a set of named constant integer. Using the enum keyword, we can declare an enumeration type with using the enumeration tag (optional) and a list of named integer.

An enumeration increases the readability of the code and easy to debug in comparison of symbolic constant (macro).

Most important thing about the enum is that it follows the scope rule and compiler automatic assign the value to its member constant.

Note: A variable of enumeration type stores one of the values of the enumeration list defined by that type.

Syntax of enum

enum Enumeration_Tag { Enumeration_List };

WHERE,
The Enumeration_Tag specifies the enumeration type name.

The Enumeration_List is a comma-separated list of named constant.

Example,

enum FLASH_ERROR { DEFRAGMENT_ERROR, BUS_ERROR };

 

Declaration of enum in c language

In below example, I have declared an enum of day type, by default, all member of the enum would be the integer constant. If we do not initialize the member of the list with a value then compiler automatic assign the value to each member of the list in the increasing order.

Note: By default compiler always assign 0 to the first constant member of the enumeration list.

Example of enum in c

The following example describes the functionality of enum in c.

OutPut:

Mon = 0 Tue= 1;

In above example, we have seen, how to use enum in a program. I have already discussed, if we do not assign the value to the enumeration constant then compiler automatically assign the 0 to the first member of the list and increase these value by 1 in a forward sequence for each member of the list.
To verify the above statement, I have created a variable of enumeration day type and assign it, two consecutive constant members Mon, Tue and print the value of this variable.

 

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

Seven important points of enumeration in c language

  • In enumeration, the set of enumeration constant may contain a duplicate value. In another word we can say, two constant members may have the same value. For example, in below example ERROR and FLASH_ERROR have same value 0.

 

  • An enumeration obeys the scope rules. For example, if we compile the below code,then the compiler does not through any error.

OutPut:

ERROR = 0;

But when the enumeration tag come within the same scope then compiler throw the compiler error.

[Error] redeclaration of ‘enum ERROR_LIST

  • In enumeration, we can also declare an unnamed enumerator data type, means that we can omit the enumeration tag.

  • In enumeration, every identifier of the list or variable should have the unique name within the scope. For example, there is two enumerations type ERROR_LIST and ERROR_LIST_INFO, both have the same identifier ( ERROR ). So when we compile that code we will get a compiler error.

[Error] redeclaration of enumerator ‘ERROR’
recipe for target ‘main.o’ failed

  • An enumeration tag also follows the scope rules. So enumeration tag should be different from the tag of structure, union or the enumeration type.

[Error] ‘ERROR_LIST’ defined as wrong kind of tag

  • If we do not assign the value to the enumeration constant then compiler automatically assign the value to the enumeration constant.

 

We can assign the value to the enumeration constant in any order, unassigned constant get the value from the previous constant and plus one.

OutPut:
Mon = 10
Tue = 11
Thu = 0
Fri = 1

  • The value assigned to the enum member should be integral constant and within the range of integer.

OutPut:

ERROR = 1410065407 (Not getting actual value)

Your opinion matters

Although here, I have tried to discuss a lot of points regarding enumeration constant but I would like to know your opinion on enumeration constant. So please don’t forget to write a comment in the comment box.

 


6 comments

Leave a Reply