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.
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.
typedef int Inter;
In this example my typedef int Inter; Now I can use int as a new name Inter in my program.
typedef int Inter;
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.
Enter Two Numbers
P + Q = 320
- 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.
Note -: type_name is the new name given to this structure.
typedef struct Prog
printf(“Enter Book Records\n”);
printf(“Book_id = “);
printf(“The Book Title = “);
printf(“The Book price = “);
printf(“\n Book ID = %d, Book Title = %s, Book Price = %f”, b1.book_id, b1.title, b1.price);
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.
- 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.
- 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.
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.
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.