The typedef is a reserved word (keyword) in the C programming language. It is used to create an additional name for another existing data type. It does not create a new data type rather give a simple alias to the existing data type so it can be easily referred in the program. In this guide, you will learn how to define and use typedef in C.
Syntax of typedef
typedef existing_data_type, alias
For example:
typedef unsigned int pnum
Here an alias of unsigned int is created with the alias name “pnum”. Once this alias is created, you can use it as shown below:
You can declare unsigned int variables like this:
pnum num1, num2;
Instead of declaring the variable like this:
unsigned int num1, num2;
Let’s take an example to understand this concept:
#include <stdio.h>
int main()
{
//creating an alias for unsigned int
typedef unsigned int qty;
//this is same as: unsigned int num, num2, num3;
qty num,num2, num3;
num=10;
num2 = 100;
num3 = 16;
printf("Value of num: %d",num);
printf("\nValue of num2: %d",num2);
printf("\nValue of num3: %d",num3);
return 0;
}
Output:
Value of num: 10 Value of num2: 100 Value of num3: 16
Using typedef in the structures
The main use of typedef is while working with structures. This is because structure often have long statement which can be made shorter and simple to read using typedef. Let’s see how can we use typedef in structures to make them more user friendly.
struct employee
{
int id;
char name[30];
int contact;
char address[100];
};
The above declaration created a structure employee. Now if you want to create a structure variable, you can do it like this:
struct employee e1, e2;
By using typedef, we can simplify this process and make it easy to read.
struct employee
{
int id;
char name[30];
int contact;
char address[100];
};
typedef struct employee emp;
emp e1, e2;
Now emp
represents the struct employee
and you can simply declare structure variable using “emp
“.
The above declaration can be written in more simple form like this:
typedef struct employee
{
int id;
char name[30];
int contact;
char address[100];
}emp;
emp e1, e2;
Let’s see the complete example:
#include <stdio.h>
typedef struct employee
{
int id;
char name[30];
int contact;
char address[100];
}emp;
int main()
{
emp e1;
printf("Enter data for employee");
printf("\nEnter id: ");
scanf("%d",&e1.id);
printf("Enter name: ");
scanf("%s",&e1.name);
printf("Enter contact: ");
scanf("%d",&e1.contact);
printf("Enter address: ");
scanf("%s",&e1.address);
printf("Details of employee: ");
printf("\nId: %d", e1.id);
printf("\nName: %s", e1.name);
printf("\nContact: %d",e1.contact);
printf("\nAddress: %s", e1.address);
return 0;
}
Output:
Using typedef with the pointers in C
We can also create an alias for pointers using typedef.
Pointers are declared like this:
float* p1;
Create an alias for float*:
typedef float* fptr;
Now you can declare float pointers like this:
fptr p1, p2, p3;
Let’s take an example to understand this concept:
#include <stdio.h>
int main()
{
typedef float* fptr;
float n1 = 5.5,n2 = 20.65, n3 = 4.555;
fptr p1 = &n1 , p2 = &n2, p3 = &n3;
printf("n1: %.3lf, value: %.3lf, address: %p \n", n1, *p1, p1);
printf("n2: %.3lf, value: %.3lf, address: %p \n", n2, *p2, p2);
printf("n3: %.3lf, value: %.3lf, address: %p \n", n3, *p3, p3);
return 0;
}
Output: