Typedef in C Language (With Examples)
Hello friends, in today’s article we are going to talk about typedef In C Language
Today we will learn in detail about what is typedef? And how to use typedef in C language?
So without wasting time let’s first understand what is typedef In C.
What is Typedef in C
typedef is a keyword used to rename pre-defined and user-defined data types in C language.
For Example – If I want to name int data type as ” INTEGER ” then I have to use typedef for this.
Its basic syntax is something like this –
Syntax -:
typedef data_type new_name;
- typedef -: This is the keyword, which is used to rename the data type in C language.
- data_type -: Here is the data type whose name we want to change.
- new_name -: At this place, the new name we want to keep by replacing the old data type name, that new name comes at this place.
Example -:
typedef int INTEGER;
In this example i have written typedef int INTEGER; Now I can use int as a new name INTEGER in my program.
Example -:
#include<stdio.h>
void main()
{
typedef int INTEGER;
INTEGER a, b;
printf("Enter Two Numbers\n");
scanf("%d %d",&a, &b);
printf("\nSum of Two Numbers is %d", a+b);
}
In this program, we gave a new name to the int data type by doing typedef int INTEGER. When we done typedef int INTEGER, then the new name of int data type is INTEGER. Although we can also create a variable by the int keyword, but now we can also create a variable of integer type by INTEGER.
In this program, with the help of INTEGER, we have created two variables named a and b and after taking two numbers input from the user, their sum will be printed in the screen with the help of printf.
Its result will be something like this –
Output -:
Enter Two Numbers
12
20
Sum of Two Numbers is 32
Note -: If the declaration of typedef is only 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 out of function then compiler gives error.
If the declaration of typedef is outside the function then it is called global declaration. We can use global declare typedef anywhere in the program.
Typedef with Structure
Like I said above, we use typedef to change the name of pre-defined and user-defined data types. And since structure is also a user-defined data type, we can also use typedef with it.
Its basic syntax is something like this –
Syntax -:
typedef struct
{
data_type member1;
data_type member2;
data_type member3;
} type_name;
Note -: type_name is the new name given to this structure.
Example -:
#include<stdio.h>
#include<string.h>
typedef struct BK
{
int book_id;
char title[200];
int price;
}Book;
void main()
{ Book b1;
printf("Enter Book Records\n");
printf("Book_id = ");
scanf("%d",&b1.book_id);
printf("Book Title = ");
scanf("%s",&b1.title);
printf("Book price = ");
scanf("%d", &b1.price);
printf("\n Book ID = %d, Book Title = %s, Book Price = %d", b1.book_id, b1.title, b1.price);
}
Output -:
Enter Book Records
book_id = 100
Book Title = CodeWithJeetu
Book price = 299
Book ID = 100, Book Title = CodeWithJeetu, Book Price = 299
In this program, we changed the structure named BK to the name of Book with the help of typedef. After this, we took the record of a book as a member variable and got its value printed on the screen.
Note –
When we change the name of struct data type with the help of typedef, then we do not have to use struct keyword with this new data type. Whereas with normal struct data type we had to use struct keyword.
It is used to increase the readability of the program, with the help of this we can change the name of any user-defined and predefined data type.
Typedef with pointer
Pointer is such a variable in which we contain the address of another variable, By using typedef with pointer, we can create many pointers at once. Let’s understand this with an example.
int * x, y, z;
In C language, when we have to create a pointer, then we use (*) symbol, in the above statement x is a pointer variable and y and z are normal variables.
Let’s now make some changes in the statement using typedef.
typedef int* intPtr ;
We have changed this int pointer to intPtr name, now if we create any variable with the help of intPtr then all the variables will be a pointer variable.
intPtr x, y, z;
In this example x, y, z; All three are pointer variables whereas in the earlier 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.
If you do not know about pointer well then you can read about it from here -: Pointer in C
Read More -:
- String in C
- Structure in C
- Union in C
- Enum in C
- Pointer in C
- Preprocessor In C
- File Handling in C
- Dynamic Memory Allocation in C
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about What is typedef In C? How to use typedef in C language?
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about typedef In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.