C typedef (original) (raw)
Last Updated : 15 Jun, 2026
The typedef keyword in C is used to create a new name (alias) for an existing data type. It helps make complex data type declarations easier to read and improves code readability and maintainability.
- typedef creates an alias for an existing data type.
- It simplifies the use of complex data types such as structures, pointers, and arrays.
- It improves code readability by replacing lengthy type names with shorter aliases. C `
#include <stdio.h>
typedef int Integer;
int main() {
// n is of type int, but we are using
// alias Integer
Integer n = 10;
printf("%d", n);
return 0;}
`
**Explanation: Here, the typedef creates an alias Integer for the int type. This allows us to use Integer instead of int throughout the program, making the code potentially more readable.
Syntax
typedef existing_type new_type;
where,
- **existing_type: The type that we want to alias (e.g., int, float, struct, etc.).
- **new_type: The new alias or name for the existing type.
After this declaration, we can use the _**alias_name_as if it were the real _**existing_name_in out C program.
Examples of typedef
The below examples illustrate the use of typedef for different purposes in our C program:
Define an Alias for Built-in Data Type
C `
#include <stdio.h>
// Defining an alias using typedef typedef long long ll;
int main() {
// Using typedef alias name to declare variable
ll a = 20;
printf("%lld", a);
return 0;}
`
**Explanation: In this code, typedef is used to define an alias **ll for the long long data type. The variable **a is declared using **ll as **a shorthand, and its value is printed using printf. This makes the code more readable and concise by using the alias instead of the full type of name.
Define an Alias for a Structure
C `
#include <stdio.h> #include <string.h>
// Using typedef to define an alias for structure typedef struct Students { char name[50]; char branch[50]; int ID_no; } stu;
int main() {
// Using alias to define structure
stu s;
strcpy(s.name, "Geeks");
strcpy(s.branch, "CSE");
s.ID_no = 108;
printf("%s\n", s.name);
printf("%s\n", s.branch);
printf("%d", s.ID_no);
return 0;}
`
**Explanation: In this code, typedef is used to define an alias **stu for the structure **Students. The alias simplifies declaring variables of this structure type, such as **st. The program then initializes and prints the values of the structure members name, branch, and ID_no. This approach enhances code readability by using the alias instead of the full **struct students declaration.
Define an Alias for Pointer Type
C `
#include <stdio.h>
// Creating alias for pointer typedef int* ip;
int main() { int a = 10; ip ptr = &a;
printf("%d", *ptr);
return 0;}
`
Define an Alias for Array
C `
#include <stdio.h>
// Here 'arr' is an alias typedef int arr[4];
int main() { arr a = { 10, 20, 30, 40 };
for (int i = 0; i < 4; i++)
printf("%d ", a[i]);
return 0;}
`