Typedef in C

Typedef in C

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:

It is a Keyword, by using this keyword, we can create a user-defined name for an existing data type. Generally, the typedef keyword used to create an alias name for the existing datatype.

Syntax: typedef Datatype user_defined_name

Program:
#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 in C Language with Examples

Example:
#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:

Typedef in C

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. I hope you enjoy this Typedef in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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