The typedef keyword in C is used to create an alternative name
for an existing data type. It can make complex declarations easier to read
and use.
typedef creates a new name or alias for
an existing type. It does not create a completely new data type.
typedef is a C keyword used to create another name for an
existing data type.
typedef int Number;
Now Number can be used as an alias for int.
The main purpose of typedef is to make declarations shorter,
clearer, and easier to understand.
Without typedef:
unsigned long int value;
With typedef:
typedef unsigned long int ULong;
ULong value;
The general syntax is:
typedef existing_type new_name;
For example:
typedef int Number;
Here, Number becomes another name for int.
typedef int Number;
Number age;
Number marks;
Both age and marks are declared using the alias
Number.
typedef float Price;
Price productPrice = 99.50f;
Price discount = 10.0f;
Here, Price is an alias for float.
typedef char Grade;
Grade grade = 'A';
printf("%c", grade);
The alias Grade can be used wherever a char type
is appropriate.
typedef unsigned int ID;
ID studentId = 101;
This can make a program easier to understand because the alias describes the intended purpose of the value.
One of the most common uses of typedef is with structures.
typedef struct
{
int id;
char name[50];
} Student;
Now we can declare a structure variable simply as:
Student student1;
Without typedef, a structure variable normally needs the
struct keyword.
struct Student
{
int id;
char name[50];
};
struct Student student1;
With typedef, the declaration can be shorter.
typedef struct
{
int id;
char name[50];
} Student;
Student student1;
typedef struct Student
{
int id;
char name[50];
} Student;
Now both struct Student and the typedef name
Student can be used to refer to the structure type.
The typedef keyword can also simplify union declarations.
typedef union
{
int number;
float price;
char grade;
} Data;
Now we can write:
Data value;
An enum can also be given a shorter type name using typedef.
typedef enum
{
LOW,
MEDIUM,
HIGH
} Level;
Now a variable can be declared as:
Level currentLevel = HIGH;
A typedef can also create an alias for an array type.
typedef int Numbers[5];
Numbers values = {10, 20, 30, 40, 50};
Here, Numbers represents an array of five integers.
A typedef can create an alias for a pointer type.
typedef int *IntPtr;
int number = 100;
IntPtr ptr = &number;
Here, IntPtr represents int *.
typedef int Number;
Number a = 10;
Number b = 20;
Number c = 30;
Once the alias is created, it can be reused throughout the program.
Meaningful aliases can make source code easier to understand.
typedef unsigned int StudentID;
StudentID id = 101;
The name StudentID communicates the purpose of the variable
more clearly than simply writing unsigned int.
A typedef name is an alias for an existing type.
typedef int Number;
Number x = 10;
int y = 20;
Number is an alternative name for int.
It does not create a completely separate integer type.
A typedef can be combined with type qualifiers such as
const.
typedef int Number;
const Number value = 100;
The variable value cannot be modified after initialization
through normal assignment.
Typedef names can make function declarations easier to read.
typedef int Number;
void display(Number value)
{
printf("%d", value);
}
The function parameter uses the alias Number.
One useful advanced application of typedef is creating an alias for a function pointer type.
typedef int (*Operation)(int, int);
Now Operation represents a pointer to a function that takes
two integers and returns an integer.
#include <stdio.h>
typedef int (*Operation)(int, int);
int add(int a, int b)
{
return a + b;
}
int main()
{
Operation op = add;
printf("%d", op(10, 20));
return 0;
}
The typedef makes the function pointer declaration easier to read.
typedef struct
{
int day;
int month;
int year;
} Date;
typedef struct
{
int id;
char name[50];
Date joiningDate;
} Employee;
Here, the Date typedef is used as a member type inside
Employee.
typedef struct
{
int id;
char name[50];
} Student;
typedef struct
{
int id;
float salary;
} Employee;
Student s1;
Employee e1;
Each typedef provides a convenient name for its corresponding structure.
In larger programs, typedef can simplify complicated declarations and make common data types easier to reuse.
For example:
typedef unsigned long ID;
typedef struct
{
ID id;
char name[50];
} Student;
This creates reusable and descriptive type names.
A typedef statement creates a type alias. It does not normally create an ordinary variable.
typedef int Number;
This creates the alias Number.
A variable is created separately:
Number age = 20;
Be careful when using pointer aliases because the alias itself may already
include the *.
typedef int *IntPtr;
IntPtr p1;
IntPtr p2;
Both p1 and p2 are pointers to int.
#include <stdio.h>
typedef struct
{
int id;
char name[50];
float marks;
} Student;
int main()
{
Student student;
student.id = 101;
printf("Enter name: ");
scanf("%49s", student.name);
printf("Enter marks: ");
scanf("%f", &student.marks);
printf("\nStudent ID = %d\n", student.id);
printf("Name = %s\n", student.name);
printf("Marks = %.2f\n", student.marks);
return 0;
}
This program uses typedef to create the Student type.
Using meaningful typedef names can make a program easier to maintain. If a complex type is used many times, a single alias can make declarations shorter and more consistent.
typedef unsigned long long StudentID;
StudentID id1;
StudentID id2;
The typedef keyword is commonly useful for:
Create a C program that:
StudentID for int.Student.Status.ACTIVE and INACTIVE values.This exercise combines typedef with structures and enumerations.
typedef creates an alternative name for an existing type.typedef is commonly used with structures.Question: What is the main purpose of typedef in C?