Back to: C Tutorials For Beginners and Professionals
Typedef in C Language
In this article, I am going to discuss the Typedef in C Language with Examples. Please read our previous articles, where we discussed the Enum in C. As part of this article, you will learn what is Typedef in C, and when and how to use Typedef in C Program with examples.
Typedef in C Language:
It is a Keyword, by using this keyword, we can create a user-defined name for an existing data type. The typedef is a keyword in the C programming language which is used to provide meaningful names to already existing variables inside a C program. In short, we can say that this typedef keyword is used to redefine the name of an already existing variable.
Syntax: typedef Datatype user_defined_name
Program to understand Typedef in C Language:
#include<stdio.h> #include<conio.h> typedef int myint; int main () { int x; myint y; typedef myint smallint; smallint z; printf ("enter 2 values:"); scanf ("%d %d", &x, &y); z = x + y; printf ("sum value is:%d", z); getch (); return 0; }
Output:
Typedef Example in C:
#include <stdio.h> #include<conio.h> #define MYCHAR char; typedef char BYTE; int main () { char ch1 = 'A'; MYCHAR ch2 = 'b'; BYTE ch3; ch3 = ch2 + ch1 + 20; printf ("char1:%c char2:%c char3:%c", ch1, ch2, ch3); getch (); return 0; }
Output:
By using #define, we can’t create alias name because, at the time of pre-processing, the identifier is replaced with the replacement text. #define is under control of pre-processor, typedef is under control of compiler.
In the next article, I am going to discuss Dynamic Memory Management in C language. Here, in this article, I try to explain Typedef in C Language with Examples. I hope you enjoy this Typedef in C Language article. I would like to have your feedback. Please post your feedback, question, or comments about this article.