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 ExampleAfter above typedef statement POSITIVE_INT can be used as an alias of unsigned int.
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:
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
C Program to show use of typedef keyword
#include <stdio.h> #include <string.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); return 0; }Output
Employee Name = Jack Employee Age = 30 Employee Salary = 10000