18 - C Typedef

This chapter introduces typedef format of C language. This is a simple topic. Type definition (typedef) is a keyword which helps in creating new name of data type. This usually adds to readability of program. We can have short declarations instead of complex jargons. Let us study in detail about typedef. 

18.1 C Type definition (typedef)

typedef keyword is used in C programming language to create a different or new name/names for an already existing data type.

These new names are called user-defined data types. Format of typedef is as follows:

typedef datatype newname_1, newname_2;

Explanation: Here,

            typedef is the keyword

            datatype is an already existing data type

            newname_1, newname_2 are the new names created for the existing data type datatype 

Let us consider an example to make things crystal clear.

 typedef float SALARY, IDENTITY;

Explanation: Here,

               typedef is the keyword

                float is one of the basic data types

                 SALARY, IDENTITY are the new names of data type provided to basic data type float.  These are called user-defined data types.

 

As we are already aware of the fact that after defining, next step is to declare them so let us do it now.

Let us consider the above typedef and learn the usage of typedef. Consider the following declaration:

        float basic, gross, monthly;

        float employee_id, college_id,student_id;

By the usage of user-defined data types, above declaration can be modified as follows: 

           typedef float SALARY, IDENTITY;

           SALARY basic, gross, monthly;

           IDENTITY employee_id, student_id, college_id;     

Let us consider sample program to explain things better:

        /* Program to demonstrate typedef */

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <conio.h>

void main()
{
   clrscr();

   struct employee
  {
    char name [10];
    int employee_id;
    char department[10];
    float salary;
  };

  typedef struct employee EMP;
  EMP e1, e2;

  strcpy(e1.name, "DEBASIF");
  strcpy(e1.department, "MANAGEMENT"):
  e1.employee_id=546502;
  e1.salary=50000;

  printf("Name of employee is %s\n", e1.name);
  printf("Employee id is %d\n", e1.employee_id);
  printf("Employee salary is %d\n", e1.salary);
  prntf("Employee department is %s\n", e1.department);

  printf(“\n\n”);

  strcpy(e2.name, "ASMITA");
  strcpy(e2.department, "ACCOUNTS"):
  e2.employee_id=546503;
  e2.salary=40000;

  printf("Name of employee is %s\n", e2.name);
  printf("Employee id is %d\n", e2.employee_id);
  printf("Employee salary is %d\n", e2.salary);
  prntf("Employee department is %s\n", e2.department);

  getch();
}

Program will looks similar in editor as in following snapshot:
Program to demonstrate typedef

                                         Figure - Program to demonstrate typedef   

After compiling program looks similar as in following snapshot:

    Program to demonstrate typedef after compiling                 

                                  Figure -  Program to demonstrate typedef after compiling
   Output of program will look similar as in following snapshot:

                            Figure - Output of program to demonstrate typedef 

We conclude chapter with this and next chapter will introduce typecasting. Thank you. 

Like us on Facebook