enum in c, seven important points you should know

enum in c

An enum in C is a user-defined data type and it consists 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.

Basically, we used the enum to increase the code readability and with enum easy to debug the code as compared to symbolic constant (macro). The most important property of enum is that it follows the scope rule and the compiler automatically assigns 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 in C:

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.

 

enum in C Example,

enum FLASH_ERROR { DEFRAGMENT_ERROR, BUS_ERROR };

In the above example, FLASH_ERROR is Enumeration_ Tag and DEFRAGMENT_ERROR,  BUS_ERROR is the named constant of the Enumeration_List.

 

Declaration of enum in c language

In the below example, I have declared an enum of day type, by default, all members of the enum would be the integer constant. If we do not initialize the member of the list with a value then compiler automatic assigns 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.

 

enum Day 
{ 
 Mon,
 Tue,
 Wed,
 Thu,
 Fri,
 Sat,
 Sun 
};

 

Example of enum in C

The following example describes the functionality of enum in C.

#include <stdio.h>

int main(int argc, char *argv[])
{
    enum Days { Mon,Tue,Wed,Thu,Fri,Sat,Sun }; //declaration of enum in c

    enum Days eDay = Mon; //Assign Mon to enumeration variable

    printf("Mon = %d\n",eDay);

    eDay = Tue;  //assign

    printf("Tue = %d\n",eDay);

    return 0;
}

OutPut:

Mon = 0 Tue= 1;

In the above example, we have seen, how to use enum in C programming. I have already discussed, if we do not assign the value to the enumeration constant then compiler automatically assigns 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. You can see above mentioned code where I have created a variable of enumeration day type and assign it two consecutive constant members Mon, Tue and print the variable.

 

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

Your free trial is waiting

 

Seven important points of enumeration in C language

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

#include <stdio.h>


int main(int argc, char *argv[])
{
    //declaration of enum
    enum ERROR_LIST { ERROR =0,LOG_ERROR,FLASH_ERROR=0};

    //Assign ERROR to enumeration variable
    enum ERROR_LIST eGetError = ERROR;

    printf("ERROR = %d\n",eGetError);

    return 0;
}

 

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

#include <stdio.h>

//declaration of enum
enum ERROR_LIST { ERROR_INFO =0,LOG_ERROR_INFO,FLAS_ERROR_INFO=0};

int main(int argc, char *argv[])
{
    //declaration of enum
    enum ERROR_LIST { ERROR =0,LOG_ERROR,FLAS_ERROR=0};

    //Assign ERROR to enumeration variable
    enum ERROR_LIST eGetError = ERROR;

    printf("ERROR = %d\n",eGetError);

    return 0;
}

OutPut:

ERROR = 0;

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

#include <stdio.h>

int main(int argc, char *argv[])
{
    //declaration of enum
    enum ERROR_LIST { ERROR =0,LOG_ERROR,FLAS_ERROR=0};
    //declaration of enum
    enum ERROR_LIST { ERROR_INFO =0,LOG_ERROR_INFO,FLAS_ERROR_INFO=0};
    //Assign ERROR to enumeration variable
    enum ERROR_LIST eGetError = ERROR;

    printf("ERROR = %d\n",eGetError);

    return 0;
}

[Error] redeclaration of ‘enum ERROR_LIST

 

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

#include <stdio.h>


int main(int argc, char *argv[])
{
    //declaration of enum
    enum  { ERROR =0,LOG_ERROR,FLAS_ERROR=0} ERROR_LIST;

    //Assign Mon to enumeration variable
    ERROR_LIST  = LOG_ERROR;

    printf("ERROR = %d\n",ERROR_LIST);

    return 0;
}

 

4. In enumeration, every identifier of the list or variable should have a unique name within the scope. For example, there are 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.

#include <stdio.h>


int main(int argc, char *argv[])
{
    //declaration of enum
    enum ERROR_LIST { ERROR =0,LOG_ERROR,FLAS_ERROR=0};
    //declaration of enum
    enum ERROR_INFO { ERROR =0,LOG_ERROR_INFO,FLAS_ERROR_INFO=0};
    //Assign Mon to enumeration variable
    enum ERROR_LIST eGetError = ERROR;

    printf("ERROR = %d\n",eGetError);

    return 0;
}

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

 

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

#include <stdio.h>

int main(int argc, char *argv[])
{
    //declaration of enum in c
    enum ERROR_LIST { ERROR =0,LOG_ERROR,FLAS_ERROR=0};
    struct ERROR_LIST
    {
        int  ERROR_CAUSE;
    };
    //Assign Mon to enumeration variable
    enum ERROR_LIST eGetError = ERROR;

    printf("ERROR = %d\n",eGetError);

    return 0;
}

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

 

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

#include <stdio.h>


int main(int argc, char *argv[])
{
    //declaration of enum in c
    enum  STATUS { TRUE,FALSE};

    //Assign TRUE to enumeration variable
    enum  STATUS eFlagStatus  = TRUE;
    printf("ERROR = %d\n",eFlagStatus);

    //Assign FALSE to enumeration variable
    eFlagStatus  = FALSE;
    printf("ERROR = %d\n",eFlagStatus);

    return 0;
}

 

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

#include <stdio.h>

int main(int argc, char *argv[])
{
    //declaration of enum in c
    enum Days { Mon =10,Tue,Wed,Thu = 0,Fri,Sat,Sun };

    //Assign Mon to enumeration variable
    enum Days eDay = Mon;
    printf("Mon = %d\n",eDay);

    //assign
    eDay = Tue;
    printf("Tue = %d\n",eDay);

    //Assign Mon to enumeration variable
    eDay = Thu;
    printf("Thu= %d\n",eDay);

    eDay = Fri;  //assign
    printf("Fri= %d\n",eDay);

    return 0;
}

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

 

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

#include <stdio.h>

int main(int argc, char *argv[])
{
    //declaration of enum
    enum ERROR_LIST { ERROR =9999999999,LOG_ERROR,FLAS_ERROR=0};

    //Assign Mon to enumeration variable
    enum ERROR_LIST eGetError = ERROR;
    printf("ERROR = %d\n",eGetError);

    return 0;
}

OutPut:

ERROR = 1410065407 (Not getting actual value)

 

Enum vs Macro in C (enum vs #define)

  • An enum increases the readability of the code and easy to debug in comparison to the macro.
  • All elements of enum grouped together which is not possible with macro.
//constant created by macro,

#define MON 0
#define TUE 1
#define WED 2
#define THU 3
#define FRI 4
#define SAT 5
#define SUN 6

//constant created by enum,

typedef enum Days
{
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat,
    Sun

} Weekday;
  • enum in C define new type but the macro does not define a new type.
  • enum follow scope rules and compiler automatic assigns the value to its member constant.
  • enum in C type is an integer but the macro type can be any type.

 

Use of typedef enum in C

We can use the typedef and enum together in C programming.  If we use typedef with enum in C then it increases the code readability and create a new type for the enum. Let see an example, where I am creating a list of errors using the enum and defining a new type using the typedef.

#include <stdio.h>

//typedef enum together
typedef enum
{
    NoError = 0,
    ReadError,
    WriteError,
    FlashError,
    LogError

} eErrorList;

//enum variable
eErrorList flashState;

int main(int argc, char *argv[])
{
    flashState = NoError;
    while(1)
    {
        //code
    }
    return 0;
}

 

 

Recommended Post



24 comments

Leave a Reply

Your email address will not be published. Required fields are marked *