Typedef in C Programming

Typedef is a keyword in C programming language that is used to give a new name to in built data type or user defined data types. New data type alias can later be used for variable declaration, typecasting etc just like a regular data type name.

For Example
typedef unsigned int POSITIVE_INT;
After above typedef statement POSITIVE_INT can be used as an alias of unsigned int.

POSITIVE_INT employeeCount;
Above declaration statement declares a variable “employeeCount” of type unsigned int using typedef alias POSITIVE_INT.
We can use typedef keyword to assign name to user define data types also like structures and unions.

Use of typedef to give new name to a structure

typedef struct EmployeeDetails{
    char name[50];
 int age;
 int salary;
} Employee;

Now, we can declare variables of EmployeeDetails structure as:
Employee employee_one, employee_two;
We can also give name to a structure after it’s declaration
typedef struct EmployeeDetails Employee;

Use of typedef to give name to a Union

typedef union ProductDetails{
    char name[50];
 int price;
 int rating;
} Product;

Now, we can declare variables of ProductDetails union
Product product_one, product_two;
We can also give name to a union after it’s declaration
typedef union ProductDetails Product;

C Program to show use of typedef keyword

Typedef in C Programming

#include <stdio.h>
#include <string.h>
#include <conio.h>
 
typedef unsigned long int ULI;
 
typedef struct EmployeeDetails{
    char name[50];
 int age;
 ULI salary;
} Employee;
  
int main(){
   Employee employee;
  
   strcpy(employee.name, "Jack");
   employee.age = 30;
   employee.salary = 10000;
  
   printf("Employee Name = %s\n", employee.name);
   printf("Employee Age = %d\n", employee.age);
   printf("Employee Salary = %ld\n", employee.salary);
 
   getch();
   return 0;
}

Program Output

Employee Name = Jack
Employee Age = 30
Employee Salary = 10000