¤ Home » Programming » C Tutorial » Miscellaneous data types in C

Miscellaneous data types in C

User defined data types (typedef)

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 -

typedef <type> <newtype>;

Here <type> indicates any existing data type and <newtype> refers to the new data type created by the user.

typedef int count;

The type name count can now be used to declare variables of type int, as shown below.

count i,j;

The above declaration is exactly the same as -

int i,j;

Enumerated data type

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.

enum <tag> {<identifier_1>, <identifier_2>, ... , <identifier_n>};
enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

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.

enum day {sunday = -1, monday, tuesday, wednesday, thursday, friday, Saturday};

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; 
    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; 
            printf("Student %s allowed to sit in final examination.\n", stud_name);
            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:

enum compass {north = 2, south, east = 1, west};

(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:


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.


comments powered by Disqus

Buy Domain & Hosting from the most reliable and trusted company - WebServicesWorldWide.com.

Looking to build a website?
Launch a 20 page website in 1 day at only Rs.200/month or US$ 3.19/month. Hosting & Email included.

About the Author

Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy