Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

C Programming Enumeration

October 3, 2013 Posted by: GPA Views: 26036 0 comments

Problem

What is an enum in C?

Solution

Enumeration is your own data type. It is a set of integer enumeration constants called the "enumerator constants" "enumeration set"  or "members" of enum.

Enumeration Definition

An enumeration is declared with keyword enum.

enum color{ red,green,yellow,brown,black,blue}; 

This enum definition shows that color is a enum type with constants red,green and so on. Values in an enumeration start with 0, and are incremented by 1. 

Initializing Enumeration 

In main function You can declare:

enum color colors; 

In this example we show colors using enum. 

Example:



#include <stdio.h>
#include <stdlib.h>

enum
color{red,green,yellow,brown,black,blue};

int
main(int argc, char *argv[]){
//Initializing Enumeration
enum color colors;
const char *strColors[] = {"red","green","yellow","brown","black","blue"};
for(colors=red;colors<=blue;colors++){
printf("%i->%s\n",colors,strColors[colors]);
}
system("PAUSE");//delay
return 0;
}
Output

Enumeration

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe