Typedef keyword is used to assign a new name to a type. This is used just to prevent us from writing more.You can use typedef to give a type a new name.

Lets take an example.

if we want to declare some variables of type unsigned int, we have to write unsigned int in a program and it can be quite hectic for some of us. So, we can assign a new name of our choice for unsigned int using typedef which can be used anytime we want to use unsigned int in a program.

typedef current_name new_name

typedef unsigned int uint;
uint i, j;


Now, we can write uint in the whole program instead of unsigned int. The above code is the same as writing:

unsigned int i, j;

Lets try an example.

int main() {
	typedef unsigned int ui;
	ui i = 5, j = 8;
	printf("i = %d\n", i);
	printf("j = %d\n", j);
	return 0;

i = 5
j = 8

You can use typedef to give a name to your user defined data types as well. For example, you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows .

typedef struct Books {
   char title[50];
   char author[50];
   char subject[100];
   int book_id;
} Book;
int main( ) {

   Book book;
   strcpy( book.title, "C Programming");
   strcpy( book.author, "Nuha Ali"); 
   strcpy( book.subject, "C Programming Tutorial");
   book.book_id = 6495407;
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);

   return 0;

Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407

Lets try another example.

int main()
   typedef long long int LLI;
   printf("Storage size for long long int data " \
          "type  : %ld \n", sizeof(LLI));
   return 0;

Storage size for long long int data type : 8

Close Menu