enum in c

enum is keyword of c with the help of enum we can create large numbers of constant of type int, which are known as enum constants.

Syntax:



Note. [ ] indicate that they are optional.


Example:
enum color{RED=1,GREEN=2,YELLOW=3}
c1, c2, c3;

Explanation of each term in syntax:


1. <tag_name>: It is name of set of all enum constants. Name of tag must be a valid identifier. This must be followed identifier naming rule.

IDENTIFIER NAMING RULE.


It is optional to write tag_name. For example:


enum  {RED=1,GREEN=2,YELLOW=3}
c1, c2, c3;
We can declared enum variables with the help of <tag_name>. For example:

enum color {RED=1,GREEN=2,YELLOW=3}
c1, c2, c3;
enum color c4,c5,c6;

2. <const_name>: It is name of constant which we want to make with the help enum keyword. <const_name> also must be followed identifier naming rule. It is convention to write <const_name> in the uppercase.

3.<value>: It is numeric number which we want to assign the <const_name>.

Important points about <value>


(a)Range of <value> must be range of signed int both Turbo c and Linux gcc compilers. Hence in Turbo c compiler
   -32768 < value < 32767
In Linux gcc compiler: 
  -2147383648 < value <2147483647


(b)If we will try to assign any value which is beyond the range of signed int then it will cause of a compilation error.

(c)To assign any value to enum constants is optional. For example:

enum color{RED,GREEN,YELLOW}
c1, c2, c3;

Then what will be value of enum constants RED, GREEN, YELLOW?

(d)If any value has not assigned to enum constant then value of enum constant will be:

<enum_value> = <previous_enum_value> + 1;


That is value of enum constant will be value of previous enum constant plus one and default initial value of first enum constant is zero. For example:

enum color{RED,GREEN,YELLOW,PINK = -2,BLUE}
c1, c2, c3;

Question: What will be value of each enum constants in above declaration?
Answer:
RED = 0         //Default initial value
GREEN = 1     
/**
<enum_value> = <previous_enum_value> + 1;
GREEN = 0+1 = 1               
*/
YELLOW = 2    
/**
<enum_value> = <previous_enum_value> + 1;
YELLOW = 1+1 = 2              
*/
PINK = -2
BLUE = -1
/**
<enum_value> = <previous_enum_value> + 1;
YELLOW = -2+1 = -1                
*/


(e) If we will not assign any value to enum constant then .we maximum 32768 numbers of enum constants can be created. (In turbo c) 

Question: What will be output of following c code in Turbo c compiler and Linux gcc compiler?

#include<stdio.h>
enum test{A=32766,B,C}
t;
int main(){
    printf("%d  %d  %d",A,B,C);
    return 0;
}

5. <variable>: These are enum type variables. We can assign enum constant to these variables. For example:

#include<stdio.h>
enum color{RED=5,GREEN,YELLOW,BLACK}
c;
int main(){
    for(c=RED;c<=Black;c++){
         printf("ctutorial.blogspot.com");
    }
    return 0;
}

Default initial value of first constant is zero.

Value of must be within range of integer. Example: 


#include<stdio.h>

int main(){
    enum colour { green, red=5, blue, white, yellow=10, pink };
    printf("%d %d %d %d %d %d", green, red, blue, white, yellow, pink);
    return 0;
}

Output: 0 5 6 7 10 11

4 comments:

sai krishna said...

this is the o/p of last one:
0 5 6 7 10 11

Pradeep said...

right!!!!!

Rajan said...

its wrong output..8 will not come in last question

Unknown said...

see the result man