×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Dynamic Memory Allocation malloc vs calloc -MAKAUT / WBUT MCA 2011 Binary Searching using C - MAKAUT / WBUT MCA 2011
enumerated data type in C Language - MAKAUT / WBUT MCA 2011 - C Language
1920    Arnab De    10/12/2019

What do you mean by enumerated data type in C ? Explain with an example. [WBUT MCA 2011]

Enumeration (or enum) is a ADT (Abstruct Data Type) or user defined data type in C. Enumeration is assign names (String) to its corresponding integral constants. For example, if we use multiple colors in our program, then we can assign the color name with the color code. Because colors name is easy to handle by the user but C language works with color codes.

softetechnologies

To declare an enumeration types in C language, we use a specific keyword called ‘enum’. Following is an example of enum declaration.

enum Colors{RED,GREEN,BLUE,YELLOW}; 
int main() 
{ 
   int i; 
   for (i=RED; i<=YELLOW; i++)       
   {   
          printf("%d ", i);
   }
   return 0; 
}

Output: 0 1 2 3

Note:

By default, in enumeration types, index start with 0 and increase 1 by each item.

But we can use any random value for the items. And also we can assign same number for the different item.

enum Colors{RED=4,GREEN=10,BLUE=5,YELLOW=9}; 
int main() 
{ 
   int i; 
   for (i=RED; i<=YELLOW; i++)
   {  
        printf("%d ", i);
   }
   return 0; 
}

Output:4 10 5 9

We can assign values to some item of enum in any order. All unassigned names get value as value of previous name plus one.

softetechnologies
enum Colors{RED=4,GREEN=10,BLUE,YELLOW=9}; 
int main() 
{ 
   int i; 
   for (i=RED; i<=YELLOW; i++)
   {  
        printf("%d ", i);
   }
   return 0; 
}

Output:4 10 11 9

Each enum item must be unique in a scope

enum fontColors{RED,GREEN,BLUE,YELLOW}; 
enum backcolor{CYAN,BLACK,RED,WHITE};
  
int main(){return 0;} 

Output: It generates an error because RED is declared twice in the scope. Both item are declared in different enum variable, but it is also not allowed.

Dynamic Memory Allocation malloc vs calloc -MAKAUT / WBUT MCA 2011 Binary Searching using C - MAKAUT / WBUT MCA 2011
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
Imagination is everything. It is the preview of life's coming attractions.
Albert Einstein
2040
58.5
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43984
01/01/2018     36611
25/06/2018     35914
28/06/2017     34654
02/08/2017     33153
01/08/2017     27616
06/07/2017     27336
15/05/2017     26986
14/07/2017     22627
21/04/2018     21239
softetechnologies