Operators in C | enum


An alternate method for naming integer constants is often more convenient then const. this can be achieved by creating enumeration using keyword “enum”.

For example

enum color{red,blue,green};

defines three integer constants called enumerators, and assigns values to them. Enumerator values are by default assigned in increasing order starting from 0. if any value is assigned to the constant it takes the assigned value and the next constant has the value one more than the previous one.

By default the assigned values are

const int red=0;
const int blue=1;
const int green=2;

If the declaration is
enum color{red,blue=10,green};
then the assigned values will be

const int red=0;
const int blue=10;
const int green=11;

int main()
	  enum color{red,blue,green}; 
	/*  enum color{red,blue=2,green}; */
	/*  enum color{red=-3,blue,green}; */
	/* enum color{red,blue=9,green}; */
  	color r,g,b;
	printf("red %d  blue %d gereen %d\n",r,b,g);
/* Output */
red 0  blue 1 gereen 2