Typedef in C

Introduction

typedef is a keyword in the C language. C language facilitates programmers to assign a new name to the existing data types. typedef keyword is used for the purpose of assigning a new name to a type.

It reduces the workload of programmers as it prevents them from writing more.

For example, we want some variables of type unsigned int. Suppose the variables are a, b, c, d, e, f. Then the declaration statements for these variables will be like

unsigned int a;

unsigned int b;

unsigned int c;

unsigned int d;

………..

Don’t you think it is very hectic to write such a large data type again and again? But we have one more way to overcome this complex syntax.

We can use the typedef keyword to assign a new name of our choice to unsigned int. That particular name can be used anywhere in the program in place of ‘unsigned int’.

Syntax

The syntax to be used while using typedef keyword is

typedef current_name new_name;

Example:

typedef unsigned int unit;

unit a, b, c, d, e, f;

The above-written code changes the name of ‘unsigned int’ to ‘unit’. Thus, we use the ‘unit’ type to declare the variables that are a, b, c, d, e, f.

Program

#include<stdio.h>
int main()
{
typedef unsigned int unit;
unit i=10;
unit j=20;
printf("i=%d\n",i);
printf("j=%d\n",j);
return 0;
}

 

Output

The above-written code works fine. So, we can use the typedef keyword to change the name of any data type.

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.