In this article Typedef Keyword in C we give the information about typedef is a keyword used to give new names to pre-defined and user-defined data types in C language.

Typedef Keyword in C:-

typedef is a keyword used to give new names to pre-defined and user-defined data types in C language.

For Example – If I want to name int data type as “Inter”, then for this I have to use typedef.

Syntax -: 

typedef data_type new_name;

typedef: This is the keyword which is used to change the name of the data type in C language.

data_type: Here is the data type whose name we want to change.

new_name: The new name given by changing the name of the old data type comes in this place.

Example -:

typedef int Inter;

In this example my typedef int Inter; Now I can use int as a new name Inter in my program.

Example -:

#include<stdio.h>

void main()

{

typedef int Inter;

Inter p,q;

printf(“Enter Two Numbers\n”);

scanf(“%d %d”,&p, &q);

printf(“\n P + Q  = %d”, p+q);

}

In this program we have defined int data type as typedef int INTEGER; Gave it a new name. When we typedef int INTEGER; Then the new name of int data type became INTEGER. Although we can create variables through int keyword, now we can also create integer type variables through INTEGER.

In this program, we have created two variables named a and b with the help of INTEGER and after taking the input of two numbers from the user, their sum has been printed on the screen with the help of printf.

Output -:

Enter Two Numbers

120

200

P + Q =  320

Note -:

  • If the declaration of typedef is inside a function or main() function, then it is called local declaration and we can use it only inside that function or block. If we try to use local declare typedef outside the function, the compiler gives an error.
  • If the declaration of typedef is twelve times that of the function then it is called global declaration. We can use global declare typedef anywhere in the program.

Typedef with Structure

As I said above, typedef is used to change the name of pre-defined and user-defined data types. structure is a user-defined data type, so we can use typedef with it also.

Syntax -:

typedef struct

{

data_type member1;

data_type  member2;

return_type  member3;

} type_name;

Note -: type_name is the new name given to this structure.

Example -:

#include<stdio.h>

#include<string.h>

typedef struct Prog

{

int book_id;

char title[20];

float price;

}Book;

void main()

{

Book b1;

printf(“Enter Book Records\n”);

printf(“Book_id = “);

scanf(“%d”,&b1.book_id);

printf(“The Book Title = “);

scanf(“%s”,&b1.title);

printf(“The Book price = “);

scanf(“%f”, &b1.price);

printf(“\n Book ID = %d, Book Title = %s, Book Price = %f”, b1.book_id, b1.title, b1.price);

}

Output -:

Enter Book Records

Book_id = 101

The Book Title = C Programming

The Book price = 500.000000

Book ID = 101, Book Title = C Programming, Book Price = 500.000000

In this program, the structure we had created with the name Prog was changed to the name Book with the help of typedef. After this, we input the records of a book in the member variable and printed their values ​​on the screen.

Note -:

  1. When we change the name of a structure data type with the help of typedef, then we do not have to use struct keyword with this new data type. Whereas with normal structure data type we had to use struct keyword.
  2. It is used to increase the readability of the program. With its help, we can change the name of any user defined and predefined data type.

Typedef with Pointer:-

A pointer is a variable in which we contain the address of another variable. With the pointer, when we use typedef, we can create many pointers simultaneously. Let us understand this with an example.

Int *a,b,c;

In C language, when we have to create a pointer, we use (*) symbol. In the above statement, a is a pointer variable and b and c are normal variables.

Let us now make a slight change in the statement using typedef.

typedef int* intPtr;

We have changed this int pointer to the name intPtr. Now if we create any variable with the help of intPtr then all the variables will be a pointer variable.

intPtr a,b,c;

In this example a, b, c; All three are pointer variables whereas in the previous example only x was a pointer variable. So in this way now we can create as many pointers as we want with the help of this intPtr.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *