16.14 – enum type in C language

by subbu on October 2, 2014

It is a user defined type used to define integer type symbolic constants, the values of which are automatically initialized by the compiler. The value of first symbolic constant would be assigned with 0, second would be 1 and third would be with 2 and so on..

enum syntax

enum types
{
 dom,com,agr,ind       /* must not be terminated with ; */
};

Here dom, com, agr and ind are symbolic constants and automatically assigned with 0, 1, 2 and 3 in a sequence. These symbolic constants can be directly used in the program without creating any variables belongs to enum (instantiating).

#include<stdio.h>
enum types
{
 dom,com,agr,ind
};
int main()
{
 printf("%d\n%d\n%d\n%d\n",dom,com,agr,ind);
 return 0;
}

Output
0
1
2
3

Though symbolic constants are automatically assigned by the compiler, we can change the value of a symbolic constant by initializing with another value but the new sequence will be continued from assigned symbolic constant.

enum types
{
 dom,com=10,agr,ind
};

Here the value of dom is automatically initialized with 0 by the compiler, the value of com is explicitly assigned with 10, hence agr and ind are automatically initialized with 11 and 12 in a sequence.

#include<stdio.h>
enum types
{
 dom,com=10,agr,ind
};
int main()
{
 printf("%d\n%d\n%d\n%d\n",dom,com,agr,ind);
 return 0;
}

Output
0
10
11
12

enum symbols can’t be explicitly assigned in the program

As we know enum is a set of integer type of symbolic constants, we can change their values within the enum definition but can’t be change in the program by assigning, which results compilation error.

#include<stdio.h>
enum types
{
dom,com=10,agr,ind
};
int main()
{
/* assigning symbolic constants */
dom=45;
agr=55;
ind=60;
printf("%d\n%d\n%d\n%d\n",dom,com,agr,ind);
return 0;
}

Output
Compilation error

Instantiating enum

We can create a variable belongs to enum and can be initialized/assigned with enum type symbolic constants.

It is also possible to assign enum type variable with any other constant of any types but only integer part is assigned because enum is a set of integer symbolic constants.

#include<stdio.h>
enum types
{
 dom,com=10,agr,ind
};
int main()
{
 enum types a,b,c,d;
 /* Assigning variables of enum */
 a=ind;
 b=45;
 c='a';
 d=45.67;
 printf("%d\n%d\n%d\n%d\n",a,b,c,d);
 return 0;
}

Output
12
45
97
45

Size of enum

Though enum practically wont takes any memory, shows the size of int.

#include<stdio.h>
enum types
{
 dom,com,agr,ind
};
int main()
{
 printf("Size of enum: %d Bytes\n",sizeof(enum types));
 return 0;
}

Output in Turbo C
Size of enum: 2 Bytes

Output in gcc
Size of enum: 4 Bytes

Where do we use enum

enum is generally used where number of constants has to use in an application and has to modify frequently whenever business logic changes.

Now let us see a couple of such cases

Specification: Accept the type of electrical connection, current month reading, previous month reading and print the number of units and bill

#include<stdio.h>
enum types
{
 dom=1,com,agr,ind
};
float getbill(int,enum types);
int main()
{
 int cmr,pmr,nu;
 float bill;
 enum types ch;
 printf("1.Domestic\n2.Commercial\n3.Agreculture\n4.Industrial\nEnter the choice 1..4:");
 scanf("%d",&ch); /* choice is accepting into enum type variable */
 printf("Current Month Reading:");
 scanf("%d",&cmr);
 printf("Previous Month Reading:");
 scanf("%d",&pmr);
 nu=cmr-pmr;
 bill=getbill(nu,ch); /* sending choice as argument */
 printf("Number of units %d\nTotal Bill %f",nu,bill);
 return 0;
}
float getbill(int nu,enum types c)
{
 switch(c)
 {
   case dom:
     return nu*3.45;
   case com:
     return nu*7.50;
   case agr:
     return nu*1.25;
   case ind:
     return nu*12.50;
 }
 return 0;
}

Execution
1.Domestic
2.Commercial
3.Agreculture
4.Industrial
Enter the choice 1..4:2
Current Month Reading:500
Previous Month Reading:100
Number of units 400
Total Bill 3000.000000

Application of enum in Turbo C (Text mode)

In Turbo C “colors” is a predefined enum defined in a header file called “conio.h”. It defines number of symbolic constants to set either the foreground or background color text mode.


enum colors{BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE}

Either numeric value or symbolic name can be used to set the color.

Setting the foreground (text) color

textcolor() is the function used to set the foreground (text) color in the active window. We can use either numeric value or symbolic name to set the text color.

textcolor(RED);   /* set the foreground (text) color to red */
textcolor(14);   /* change the foreground (text) color to yellow */

Printing colored text

Generally the printf() statement prints the text at the active cursor position only with the default foreground color white. Where as cprintf() helps to print the text with the active window color.

textcolor(14);
cprintf("Hello world"); /* prints the "Hello World" in yellow color */

Example:

#include<conio.h>
#include<stdio.h>
int main()
{
 int i,x,y;
 clrscr();
 x=5;                     /* setting column */
 y=3;                     /* setting row   */
 for(i=1;i<=15;i++)       /* changing colors */
 {
   textcolor(i);         /* setting text color */
   gotoxy(x,y);
   cprintf("Codingfox");
   y++;                 /* changing row */
 }
return 0;
}

Output:

textcolor() in Turbo C

Setting background color

textbackground() is the function used to set the background color to the active window. It accepts either numeric color value or symbolic name as argument.

Example:

#include<conio.h>
#include<stdio.h>
int main()
{
 clrscr();
 gotoxy(5,10);                   /* sends the cursor to (5,10) */
 textbackground(WHITE);         /* set the background color   */
 textcolor(BLACK);               /* set the foreground color   */
 cprintf("CodingFox");
 return 0;
}

Output:

textbackground() in Turbo C

Previous post:

Next post: