An enumeration (enum) is a user-defined type (same as structure) that represents a group of constants. typedef is a keyword used to create alias name for the existing datatypes.
2. Enumeration/enum
2
An enumeration (enum) is a user-defined type (same as
structure) that represents a group of constants.
(or)
enum is a user defined data type where we specify a set of
possible values for a variable and the variable can only take
one out of a set of possible values.
It is used to assign names to the integral constants, which
makes a program easy to read and maintain.
3. When use enum in C
3
Enums are used only when variable has one set of possible values
that are not going to change (e.g., days of the week, colors in a
rainbow, number of cards in a deck, directions, etc.).
For example, we have a direction variable that holds the direction.
Since we have four directions, this variable can take any one of the four
values, if we try to assign a another random value to this variable, it will
throw a compilation error.
This increases compile-time checking and avoid errors that occurs by
passing in invalid constants.
Another important place where they are used frequently are switch case
4. Defining an enum
4
An enum is defined with a keyword enum and the elements separated
by 'comma' as follows.
Syntax:
enum enum_name
{
element1,
element2,
…
…
elementN
};
5. Example
5
For example, Summer, Spring, Winter and Autumn are the names of four
seasons. Thus, we can say that these are of types season. Therefore, this
becomes an enumeration with name season and Summer, Spring, Winter and
Autumn as its elements.
Now let's define an enum of the above example of seasons.
enum Season
{
Summer,
Spring,
Winter,
Autumn
};
6. Values for the Members of Enum
6
All the elements of an enum have a value. By default, the value of the
first element is 0, that of the second element is 1 and so on.
Let's see an example.
#include <stdio.h>
enum season{ Summer, Spring, Winter, Autumn};
int main() {
printf("%dn",Summer);
printf("%dn",Autumn);
return 0;
}
7. Assigning custom values to enum elements
8
It is also possible to assign your custom values to enum elements by writing:
enum enum_name {
name1 = constan1/expression1,
name2 = constan1/expression2,
…
…
name N = constanN/expression
};
Example:
enum TV { FOX = 11, CNN = 25, ESPN = 15, HBO = 22, SONY = 30, NG = FOX+CNN };
printf(" FOX: t%2dn", FOX);
printf(" CNN: t%2dn", CNN);
printf(" NG: t%2dn", NG);
8. 9
We can assign values to some name in any order. All
unassigned names get value as value of previous name
plus one.
#include <stdio.h>
enum rainbow {violet = 1, indigo, blue = 5,green, yellow=12, orange, red};
int main()
{
printf("%d %d %d %d %d %d %d", violet, indigo, blue, green, yellow,
orange, red);
return 0;
}
Output:
9. 10
Two enum names can have same value.
For example, in the following C program both ‘Failed’ and ‘Freezed’ have same
value 0.
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};
int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}
Output:
1, 0, 0
10. 11
All enum constants must be unique in their
scope.
For example, the following program fails in
compilation.
enum state {working, failed};
enum result {failed, passed};
int main()
{
return 0;
}
11. Questions?
12
Write a C program to create enumerated data type for 7 days and
display their values in integer constants.
Write a C program to create enumerated data type for 7 days.
When you provide an enumerator as input to switch case, it should
display like “Today is monday”.
12. typedef in C
13
typedef is a keyword used to create alias name for the existing
datatypes.
Using typedef keyword we can create a temporary name to the system
defined datatypes and user – defined datatypes like structure, unions,
arrays,etc.
We use that temporary name to create a variable. The general syntax of
typedef is as follows...
typedef <existing-datatype> <alias-name>;
Example:
typdef int Number;
In the above example, Number is defined as alias name for integer
datatype. So, we can use Number to declare integer variables.
13. Example Program to illustrate typedef in C.
14
#include<stdio.h>
typedef int Number;
int main()
{
Number a,b,c; // Here a, b and c are integer type of variables.
printf("Enter any two integer numbers: ") ;
scanf("%d%d", &a,&b) ;
c = a + b;
printf("Sum = %d", c) ;
return 0;
}
14. typedef with structures
15
You can use typedef to give a name to your user defined data types as well.
For example, you can use typedef with structure to define a new data type
and then use that data type to define structure variables directly as follows −
#include<stdio.h>
typedef struct employee
{
char name[50];
int salary;
}emp;
int main( )
{
emp e1;
printf("Employee name:");
scanf("%s", e1.name);
printf("Enter Employee salary:");
scanf("%d", &e1.salary);
printf("nStudent name :%sn", e1.name);
printf("roll no: %d", e1.salary);
return 0;
}
15. typedef with Arrays
16
typedef is also used with arrays.
Consider the following example program to understand how typedef is
used with arrays.
int main(){
// Here Array acts like an integer array type of
size 5.
typedef int Array[5];
// List is an array of integer type with size 5.
Array list ={10,20,30,40,50};
int i;
printf("List elements are : n") ;
for(i=0; i<5; i++)
printf("%dt", list[i]) ;
return 0;
}