Typedef in C
“In C language typedef keyword can be used to give an existing data type a new type name. It enables you to give a data type with a new name, which might make your code easier to read and maintain”.
Typedef is used to define user-defined datatypes, which function similarly to creating a command alias.
Typedef syntax in C
typedef old _ data _ type new _ data _ type _ name;
parameters
old _ data _ type: This parameter refers to any data type in C, such as int, float, char, or any user-defined data type.
New _ data _ type _name: It represents the new name that you want to create for the existing data type.
Example
For the current data type int, you may use typedef as follows to create a new type name called myIntvariable:
typedef int myIntvariable;
Now, you can use myIntvariable anywhere in your code to replace int. For instance:
myIntvariable numvar = 100;
Here, numvar is a variable of type myIntvariable, which is equivalent to int. This can help make your code more readable and understandable.
Typedef can also be used with complex types such as structures and pointers, allowing you to create new names for complex data types as well
// C program, which is implementing typedef #include <stdio.h> typedef long long ll; void main () { // using typedef name to declare a variable ll varnum = 200; printf ("%ld", varnum); }
Output:

Typedef uses in C
The typedef in C is frequently used in the ways listed below:
- The typedef keyword gives the existing data type a meaningful name, making it easier for other users to understand the program.
- It can be used with structures to improve code readability and save us from frequently typing struct.
- Pointers can be declared in a single line using the typedef keyword to declare multiple pointers.
- Any number of variables can be declared using it in conjunction with arrays.
- typedef using array
Typedef using Array
An array data type can also be given a new name using typedef in C programming.
The following is the syntax for using typedef with an array:
typedef old _ data _type new _ data _ type
old _ data _type denotes the array's data type, new _ data _ type _name is the new name you want to give the array, and size denotes the array's total number of items.
typedef int IntArray[10];
Now, you can use IntArray anywhere in your code to replace the int array of size 10. For instance:
intArray myArray = {1, 2, 3, 4, 5,6,7,8,9,10};
Here, myArray is a variable of type IntArray, which is equivalent to int myArray[10].
Using typedef to create a new type name for an array can be particularly useful when working with complex data structures that involve arrays, such as linked lists or trees.
Example program using Array with Typedef
#include <stdio.h> typedef int Arrayvar [4]; void main () { Arrayvar tempvar = {100, 200, 300, 400}; printf ("typedef using with an array\n"); for (int i = 0; i < 4; i++) { printf ("%d ", tempvar[i]); } }
Output:

Typedef with Pointer
typedef can also be used with pointers to create new names for pointer types. Pointers are variables that hold memory addresses, and they are commonly used in C for dynamic memory allocation and passing data between functions.
The syntax for typedef with pointers is similar to that without pointers:
typedef old_pointer_type* new_pointer_type_name;
Here, old_pointer_type is an existing pointer type, such as int* or char*, and new_pointer_type_name is the new name you want to create for this pointer type.
typedef int* myIntvarPtr;
Now, you can use myIntvarPtr to declare variables that hold the memory address of an integer, as follows
myIntvarPtr p1, p2;
above, p1 and p2 are variables of type myIntvarPtr, which is equivalent to int*. You can access and manipulate their memory address as you would with any other pointer variable.
Typedef with pointers can be useful for creating new names for pointer types that are used frequently in your code, making it easier to read and maintain
Typedef using Struct
To create a new type alias for a structure in C, combine typedef with struct. Especially if the structure is complex or has a long name, this can make it simpler to use the structure in your code.
Here's an example of how typedef can be used with a struct:
typedef struct { int numvar1; int numvar2; } Point;
In the example above, we define a new structure that symbolizes a 2D point space. There are two integer fields in the structure: numvar1 and numvar2. Then, we use typedef to make Point a new type alias that designates this structure.
Now, we can declare variables of this structure type using the Point type alias:
Point point1; point1.numvar1 = 10; point1.numvar2 = 20;
The numvar1 and numvar2 fields of a new variable of type Point named point1 are set to 10 and 20, respectively, by the code.
Code that uses typedef and struct tends to be shorter and easier to read. To make your code more legible, for instance, use the type alias to declare variables instead of always using the whole structure name:
Point point1, point2, point3;
Here, we define three variables of the type Point, point1, point2, and point3. Compared to repeatedly spelling out the complete structure name, this is a lot more succinct.
Program for typedef using struct
#include <stdio.h> typedef struct { int numvar1; int numvar2; } Point; void main () { Point point1, point2; point1.numvar1 = 10; point1.numvar2 = 20; point2.numvar1 = 30; point2.numvar2 = 40; printf ("point1 = (%d, %d) \n", point1.numvar1, point1.numvar2); printf ("point2 = (%d, %d) \n", point2.numvar1, point2.numvar2); }
Output:
