The typedef keyword allows you to define new data types having meaningful names. The new data type thus introduced can be used to declare variables, arrays, structures etc. in the usual manner. The syntax for defining new type name is -
Here <type> indicates any existing data type and <newtype> refers to the new data type created by the user.
The type name count can now be used to declare variables of type int, as shown below.
The above declaration is exactly the same as -
C supports enumerated data. Its members are constants that are defined as identifiers. These constants determine the values that can be assigned to the associated variables.
Here the members of the tag day are constants. Though these members are written as identifiers they have signed integer values. Thus a variable of type day can take up values, like sunday, monday, tuesday, etc. When you make an enumerated declaration like this, the integer values that get assigned will be 0 for sunday, 1 for monday, and so on. By default the first member will be assigned a value 0 and subsequent members will be assigned the next integer increment in order from left to right.
sunday = 0
monday = 1
tuesday = 2
wednesday = 3
...
saturday = 6
Such automatic assignment of integer values can be overridden during declaration, as shown below.
When you initialize the first member with -1, as we have said, the subsequent members will be initialized with the next integer increment. Thus, here the assigned values would be:
sunday = -1
monday = 0
tuesday = 1
wednesday = 2
...
saturday = 5
After having declared an enumerated data type as above, you can now use the data type day to define variables of type day in the usual manner, as demonstrated below.
day today, tomorrow; /* assignment */ today = wednesday; ... if(today == wednesday) tomorrow = thursday; ...
You can use typedef along with enumerated data type to create your own meaningful data types. See the example below.
typedef enum day {monday,tuesday,wednesday,thursday,friday,saturday} working_day; working_day today;
Let us illustrate the usage and relevance of enumerated data types through a full example. The following example illustrates how the concept of enumerated data can be used to create boolean data types. It may be recalled that C does not support boolean data values. In C true is interpreted as having non-zero value. In the following we have assigned value 1 to true and zero to false.
/* A student is allowed to appear in the final examination provided he has at least 75% of class attendance and has cleared the fees. This program gets each student's attendance and fee status from the user and tells whether he/she is qualified to appear in the final examination or not. */ #include <stdio.h> typedef enum bool {false = 0, true = 1} boolean; main() { boolean x, y, z; char rep = 'y', stud_name[31], fee_cleared = ''; int tot_sessions = 0, sessions_attended; float attendance; printf("\nEnter number of Sessions: "); scanf("%d", &tot_sessions); fflush(stdin); while(rep == 'Y' || rep == 'y') { x = y = false; printf("\nEnter Student Name: "); gets(stud_name); fflush(stdin); do { printf("\nEnter sessions attended (Total Sessions %d): ", tot_sessions); scanf("%d", &sessions_attended); ffiush(stdin); if(sessions_attended > tot_sessions) printf("Wrong Input\n"); } while(sessions_attended>tot_sessions); attendance = ((float)sessions_attended * 100)/tot_sessions; printf("\nPercentage of Attendance: %f\n", attendance); if(attendance >= 75) x = true; printf("\nAre fees cleared? (Y/N): "); scanf("%c", &fee_cleared); fflush(stdin); if(fee_cleared == 'Y' || fee_cleared == 'y') y = true; z = x && y; if(z) printf("Student %s allowed to sit in final examination.\n", stud_name); else printf("Student %s not allowed to sit in the examination.\n", stud_name); printf("Continue for another Student? (Y/N): "); scanf("%c", &rep); fflush(stdin); } }
(1) In the following enumeration declaration, determine the value of each member:
(2) Define an enumeration type called money, having the following members - panch_paisa, das_paisa, bis_paisa, panchis_paisa, panchas_paisa and ek_rupiyah. Assign the following integer values to these members:
panch_paisa | 5 |
das_paisa | 10 |
bis_paisa | 20 |
panchis_paisa | 25 |
panchas_paisa | 50 |
ek_rupiyah | 100 |
Define an enumeration variable called coin, of type money. Assign the initial value panchis_paisa to coin.
(3) Write a C program to accept a number (up to 4 digits) from the user and display the number in words, e.g. if user inputs 1256, the display will be one thousand two hundred fifty six.
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
Elements of the C language - Operators and Expressions
Arrays in C Part 1 of 2 - Basic array declaration and manipulation
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
How to pass Structure as a parameter to a function in C?
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.
Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.