Friday, 13 July 2012

enum in C and C++

The C/C++ enumeration syntax exists to support the use of human readable character names to support a specific list of available values for the specified variable. This is enabled in C and C++ as a set of named integer constants. This can be expressed as a C or C++ enumerated type "enum". Both C and C++ use the same enumeration syntax.

You could use enum as below.
        enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days;

Here is a code snippets in C++ that define a variable of type enum.

#include <iostream>
using namespace std;

main()
{
   enum
   {
      monday, tuesday, wednesday, thursday, friday, saturday, sunday
   } day;

   day = wednesday;

   if(day == saturday || day == sunday)
      cout << "Day is a weekend day" << endl;
   else if(day == wednesday)
      cout << "Day is hump day - middle of the work week" << endl;
}

You can access enum members directly by writing

printf("%d",monday);
in C
or 
cout<<monday;
in C++.

We can also create a enum type of which we can declare variables as and when desired:


#include <iostream>
using namespace std;

main()
{
   enum TEENS
   {
      thirteen = 10,
      fourteen,
      fifteen,
      sixteen,
      seventeen,
      eighteen,
      nineteen
   } ;

   TEENS teen = seventeen;

   if(teen == seventeen)
      cout << seventeen << endl;
}

or 

#include <iostream>
using namespace std;

main()
{
   typedef enum
   {
      thirteen = 10,
      fourteen,
      fifteen,
      sixteen,
      seventeen,
      eighteen,
      nineteen
   } TEENS;

   TEENS teen = seventeen;

   if(teen == seventeen)
      cout << seventeen << endl;
}

Now question is why to prefer enum over #define.
Here are few reasons :
1. Prefer Enums Over #define Macros When You Need a Fixed Set of Values

#define JAN 1
#define FEB 2
//...
#define DEC 12

Enum types are a significantly better choice:

 //file enums.h
enum Months {
//a list of enumerators:
Jan,
Feb,
//...
Dec };
enum Days {
Sun,
Mon,
//...
};

One more point to note is that C++ compiler has strong type check for enum where as C compiler doesnt.

Here in our example below: 

#include <stdio.h>
//using namespace std;

int main()
{
   typedef enum
   {
      thirteen = 10,
      fourteen,
      fifteen,
      sixteen,
      seventeen,
      eighteen,
      nineteen
   } TEENS;

   TEENS teen=10;
      printf("%d\n%d",seventeen,teen);
        return 0;
}

we are directly assigning 10 to teen and C compiler compiles it without giving an error and gives output 
14
10

where as in C++ with similar code: 


#include <iostream>
using namespace std;

int main()
{
   typedef enum
   {
      thirteen = 10,
      fourteen,
      fifteen,
      sixteen,
      seventeen,
      eighteen,
      nineteen
   } TEENS;

   TEENS teen=10;
   cout<<seventeen<<teen;
   return 0;
}

gives error : 

enum.cpp:17: error: invalid conversion from int to main()::TEENS

With C++  Compiler, strong type check gives much advantage. The compiler checks that an enum is always assigned a valid value (one of the enumerators in the enum's definition and no other). Enum types behave like any other built-in type so you can use them to overload functions: 



#include “enums.h”

bool func(Months month); //1

bool func(Days day); //2

void main() {

Days day = Sun; //Type Safety. Mind that ‘day = 1’ is illegal
Months month = Feb;
bool b = func(day); // func() #1 is called
b = func(month); //now func() #2 is called
}

This feature also eliminates silly mistakes like this:

bool b= func(50); //If we used #defines, this would pass unnoticed.
//Enums ensure that this mistake is detected at
//compile time


2. C++ enums are very efficient because they are automatically converted by the compiler to plain ints. Furthermore, an enum in C++ needn't be identical in size to sizeof(int). As a result, the compiler may optimize memory usage by storing the enumerators’ list in units smaller than int, e.g., short or char. There is also the possibility of storing the enum's value on a machine register, which may considerably increase performance even more. 



0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top