C Typedef with Examples
The C programming language supports various keywords and data types. In C, you can also create your own data type. Typedef is a predefined keyword. This keyword helps in creating a user defined name for an existing data type.
What is the typedef keyword in C?
As discussed, it is a predefined keyword. With the help of this keyword, you can give a meaningful name to an existing variable in your C program. To put it short, you can replace the name of the existing data type with the name which you have provided. It works similarly when we define aliases for the commands.
Working of C typedef
To implement the typedef keyword successfully, you will have to give a meaningful name to an existing data type in the program code. Then the compiler replaces the existing data type with the name that you have provided for the entire application.
Syntax1:- Using typedef keyword in C
typedef <existing data type or keyword> <user given name for the datatype or keyword>
Syntax2:- Working of typedef
typedef struct { data_type member1; data_type member2; data_type member3; }type_name;
Example:- typedef keyword in C
typedef unsigned int length;
In the above example, we have declared an unsigned int type variable length with the help of typedef keyword. Now we can create unsigned int type variables with length like below:-
length i,j;
Suppose, you have to declare an unsigned int datatype at various locations in your program code. It will be lazy work. But the typedef keyword will make your work easy.
Basic Example of C typedef keyword
#include<stdio.h> typedef int ainT; //declared a new variable of int data type! int main () { printf("TechVidvan Tutorial: Example of typedef keyword!\n\n"); int a=10; ainT b=50,c; c = a + b; printf ("Sum is: %d", c); return 0; }
Output:-
TechVidvan Tutorial: Example of typedef keyword!
Sum is: 60
Example of typedef in C
Suppose, you want to create a record of a student which includes student details such as name, id, subjects, marks, phone number etc. then you can do all this by defining a structure. It helps you in increasing the readability of the program code.
#include<stdio.h> typedef struct details { int age; int id; }student; int main( ) { student s1; printf("TechVidvan Tutorial: Example of typedef in C!\n\n"); s1.age = 21; s1.id = 3; printf("AGe of the student is: %d\n", s1.age); printf("Id of the student is: %d\n", s1.id); return 0; }
Output:-
TechVidvan Tutorial: Example of typedef in C!
AGe of the student is: 21
Id of the student is: 3
C typedef struct
You can also use the typedef keyword with structures in C. With typedef, create a new data type and then use that to define structure variables.
Example:- Using typedef with structures
typedef struct employee { int salary; int id; }emp; emp e1,e2;
Example:- typedef with structures
#include <stdio.h> typedef struct employee { int salary; int id; }emp; int main() { emp e1,e2; e1.salary = 14000; e1.id = 1; e2.salary = 12000; e2.id = 2; printf("TechVidvan Tutorial: typedef using struct!"); printf("Salary of the first employee is : %d\n", e1.salary); printf("ID of the first employee is : %d\n", e1.id); printf("Salary of the second employee is : %d\n", e2.salary); printf("ID of the second employee is : %d\n", e2.id); return 0; }
Output:-
ID of the first employee is : 1
Salary of the second employee is : 12000
ID of the second employee is : 2
In the above example, we have declared variable emp of type struct employee. We can create variables using the emp variable of type struct employee. It helps in reducing the complexity of the code.
How to use typedef in C?
In C, you can make use of the typedef keyword. It is mainly used to give meaningful names to the existing data type. Below, we have declared a real_int variable with typedef keyword.
Example:- typedef keyword
#include<stdio.h> int main() { printf("TechVidvan Tutorial: How to use typedef in C!\n\n"); typedef real_int; real_int num = 213; printf("Number is: %d\n",num); return 0; }
Output:-
TechVidvan Tutorial: How to use typedef in C!
Number is: 213
C typedef with pointers
You can also use the typedef keyword with pointers.
Example:- using typedef with pointers
typedef int* point; point a,b;
In the above example, we have declared variable point of type *int. With that, we can create variables of type *int using the point variable.
Example:- typedef with pointers
#include <stdio.h> typedef int* point; int main(){ point a1; a1 = 40; printf("TechVidvan Tutorial: typedef using pointers!\n"); printf("Value is: %d",a1); return 0; }
Output:-
Value is: 40
In the above example, a1 is the variable of type *int.
typedef and #define
#define directive works similar to the typedef keyword. #define directive is also used to define various data types like typedef keyword.
- #define directive is processed by the preprocessor and the typedef keyword is processed by the compiler.
- You have to put a semicolon at the end of a typedef keyword. But in #define, you don’t have to.
- typedef is used for giving a new name to the existing data type. And #define is a directive which is used to give an alias for values like 1 as ONE, 3.14 as PI.
Example:- #define
#include <stdio.h> #define SIDE 5 int main( ) { int area; area = SIDE*SIDE; printf("TechVidvan Tutorial: #define directive!\n"); printf("Area of square is: %d",area); return 0; }
Output:-
Area of square is: 25
Example:- typedef
#include <stdio.h> typedef char c; int main() { c c1; c1 = 'T'; printf("Printing character: %c", c1); return 0; }
Output:-
Below are some examples on how to use the typedef struct.
Example of Variable declaration without using typedef:-
#include<stdio.h> struct value{ int a; }; int main() { struct value i; i.a=5; printf("Value is: %d\n", i.a); return 0; }
Output:-
Difference between typedef and #define:-
typedef | #define |
It is compiler based. | It is preprocessor based. |
You have to put a semicolon at the end of the typedef keyword. | In #define directive, you don’t have to put a semicolon. |
It is used for giving new names to data types only. | #define is a directive which is used to give an alias for values like 1 as ONE, 3.14 as PI. |
Describes the actual definition of the new data type. | Simply copy and paste the definition values. |
Using typedef keyword
You don’t have to type struct keyword again and again in every declaration of variables.
Method One:-
#include<stdio.h> struct value{ int a; }; typedef struct value val; int main() { val i; i.a = 63; printf("Value is: %d\n", i.a); return 0; }
Output:-
Method Two:-
#include<stdio.h> typedef struct value{ int i; }val; int main() { val a; a.i = 36; printf("Value is: %d\n", a.i); return 0; }
Output:-
Application of typedef
The typedef keyword is used to give a meaningful name to the existing data type.
Use of typedef with structures:-
typedef struct { data_type variable1; data_type variable2; }variable_name;
With that variale_name, you can declare variables of structure type like below:-
variable_name a,b;
Example:- typedef unsigned char
#include <stdio.h> typedef unsigned char CHAR; int main() { CHAR letter1='T'; CHAR letter2='e'; CHAR letter3='c'; CHAR letter4='h'; CHAR letter5='V'; CHAR letter6='i'; CHAR letter7='d'; CHAR letter8='v'; CHAR letter9='a'; CHAR letter10='n'; printf("TechVidvan Tutorial: typedef with unsigned char!\n\n"); printf("Created word with alphabets: %c%c%c%c%c%c%c%c%c%c",letter1,letter2,letter3,letter4,letter5,letter6,letter7,letter8,letter9,letter10); return 0; }
Output:-
TechVidvan Tutorial: typedef with unsigned char!Created word with alphabets: TechVidvan
C Typedef using an array
You can also use the typedef keyword with an array.
Example:-
typedef int arr[10];
arr is a variable of int data type. With that, you can also declare variables as much as you can.
For example:-
arr i,j,k;
Example:- typedef with an array
#include<stdio.h> typedef int arr[5]; int main() { int a; arr i = {14,2,3,54,3}; printf("TechVidvan Tutorial: typedef using array!\n"); for(a = 0; a < 5; a++) { printf("Element of %d is: %d\n",a ,i[a]); } return 0; }
Output:-
Element of 0 is: 14
Element of 1 is: 2
Element of 2 is: 3
Element of 3 is: 54
Element of 4 is: 3
Summary:-
typedef is a predefined keyword. You can replace the name of the existing data type with the name which you have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays etc.