Enumeration or enum in C Programming - C-Tutorial

Latest

Thursday, 20 October 2016

Enumeration or enum in C Programming

Enumeration or enum in C Programming
  • ·         An Enumeration is user defined data type. It uses the keyword enum.
  • ·    An enumerated type has a finite number of named values. Each named values assigned to integer constant in ascending order.
  • ·         The symbolically declared members are known as “enumeration constants”.
  • ·         The enumeration statement should starts with the keyword enum.



            Syntax    :       enum Tag
{
Member1, Member2, - - - - - , Membern
};

            Each enumeration member is assigned with an integer value by the compiler.  By default, Member1 is given the value 0, Member2 is given the value 1 and so on.  i.e., each member value is obtained by adding 1 to the previous member value.

           
Example C program
#include <stdio.h>
enum week
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
} ;

void main()
{
printf("\nSunday = %d ", Sunday);
printf("\nWednesday = %d ", wednesday);
printf("\nSaturday = %d ", Saturday);
}

Output
Sunday : 0
Wednesday : 3
Saturday : 6

Note:

                Enumeration provides a convenient way to associate constant values with names as alternative to #define statements.

            Example:         #define TRUE 1
                                    #define FALSE 0

            An alternative representation with enumeration data type is :

            Example:         enum flag
{
FALSE, TRUE

};


ANONYMOUS ENUMERATION
The process of specifying an enumeration without tag is known as anonymous enumeration.  The general format of defining anonymous enumeration is:

Syntax             :           enum
                                    {
                                                Member1, Member2, - - - - - - , Membern
                                    };

No comments:

Post a Comment