Typedef in C Programming

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:
Program to understand Typedef in C Language
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:
Typedef in C Language with Examples

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.

Follow Us On