enum is the
abbreviation for enumerate, and we can use this keyword to declare and
initialize the sequence of integer constants. An enumerated data type is the
data type with user specified values. to declare the enumerated data type, we
write the keyword enum followed by the optional identifier of the enumerated
type called tag, followed by the list of names that are permissible values for
the data type. The values are enclosed in braces and separated by commas.
Here’s an example:
enum colors{RED,YELLOW,GREEN,BLUE};
I’ve made the
constant names in uppercase, but you can name them whichever way you want to. Here,
colors is the name given to the set of constants (remember that this name is
optional). Now, if you don’t assign a value to the constant, the default value
for the first one in the list (i.e. RED in our case) is 0. The rest of the
undefined constants have a value 1 more than the one before. So, YELLOW is 1,
GREEN is 2 and BLUE is 3. But, you are free to assign any value.
E.g. enum colors{ RED=1,YELLOW,GREEN=6,BLUE }
Now, RED=1,
YELLOW=2,GREEN=6 and BLUE=7.
The main advantage
of enum is that if you don’t initialize your constants each one would have a
unique value. The first will be 0 and the rest would be then counted upwards.
You can name your
constants in usual order if you really wanted.
enum
marital_status{SINGLE,LEARNING,DIVORCED,WIDOWED};
After enum
declaration you can declare the enum variables to access the enum members.
The following lines
declares two enum variables chap1, and chap2 of type enum marital_status.
enum marital_status chap1,chap2;
Now, we can assign
values to these variables.
chap1=LEARNING;
chap2=LICENCED;
But, we can’t use
values which are not use in the original declaration. Thus the following
expression is wrong.
chap1=NOVICE;
Remember that without
declaring enum variables, you can access the enum members directly.Look
at this tiny code:
Program 15:
#include <stdio.h>
#include <conio.h>
void main()
{
enum BOOL{FALSE,TRUE};
clrscr();
printf("%d”,TRUE);
getch();
}
Output:
#-------------------------------------------------------------------------------#
1
#-------------------------------------------------------------------------------#
Warning: enum is used to initialize the
sequence of integer constants only. So, the following declaration would be
wrong.
enum
price{ITEM1=50.60,ITEM2=99.33,ITEM3};
By
mistake, if you made this declaration in this program then compile generate “constant
expression required” error.
The
following program demonstrates the use of enum. So, do study them carefully.
Program 16:
#include <stdio.h>
#include <conio.h>
void main()
{
/* Anonymous enum (enum having no tag or
name.) */
enum
{VOILET=1,INDIGO,BLUE,GREEN,YELLOW,ORANGE,RED};
clrscr();
printf("\nDear,");
printf("\n*** Make your life colourful
with me ***");
printf("\n
Yours");
printf("\n Rainbow");
printf("\n----------------------------------------------");
printf("\n Violet is my %dst
color.",VOILET);
printf("\n Indigo is my %dnd
color.",INDIGO);
printf("\n Blue is my %drd
color.",BLUE);
printf("\n Green is my %dth color.",GREEN);
printf("\n Yellow is my %dth
color.",YELLOW);
printf("\n Orange is my %dth
color.",ORANGE);
printf("\n Red is my %dth
color.",RED);
getch();
}
Output:
#-------------------------------------------------------------------------------#
Dear,
*** Make your life colorful with me ***
Yours
Rainbow
----------------------------------------------
Violet is my 1st color.
Indigo is my 2nd color.
Blue is my 3rd color.
Green is my 4th color.
Yellow is my 5th color.
Orange is my 6th color.
Red is my 7th color.
#-------------------------------------------------------------------------------#
The following program
demonstrates the wonderful usage of enum in your program.
#include <stdio.h>
#include <conio.h>
void main()
{
int education, attitude, knowledge, hardwork;
enum ATT {A=1,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
clrscr();
printf("\nHow important the attitude
is ?\n");
printf("\nIf");
printf("\n A B C D E F G H I J K L M N
\nO P Q R S T U V W X Y Z is\n”);
printf("\n”);
printf("\n1 2 3 4 5 6 7 8 9 10 11 12
13 \n14 15 16 17 18 19 20 21 22 23 24 25
26 %");
printf("\nthen\n”);
education=E+D+U+C+A+T+I+O+N;
knowledge=K+N+O+W+L+E+D+G+E;
hardwork=H+A+R+D+W+O+R+K;
attitude=A+T+T+I+T+U+D+E;
printf(“\nEducation got %d%%”,education);
printf(“\nKnowledge got %d%%”,knowledge);
printf(“\nHardwork got %d%%”,hardwork);
printf(“\nAttitude got %d%%”,attitude);
getch();
}
Output:
#-------------------------------------------------------------------------------#
How important the attitude is ?
If
A B C D E F
J H I J K L M N
O P Q R S T
U V W X Y Z is
1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17
18 19 20 21 22 23 24 25 26
then
Education
got 92%
Knowledge
got 96%
Hardwork
got 98%
Attitude
got 100%
#-------------------------------------------------------------------------------#
No comments:
Post a Comment