What is typedef and its use in C language?

In this article we will talk about typedef in C language. I will explain what is typedef, its use and how to use it in C language.

Typedef in c programming language

Required knowledge

Basic C programming, Structures, Pointers

What is typedef in C language?

typedef is a C keyword, used to define alias/synonyms for an existing type in C language. At most cases we use typedef's to simplify the existing type declaration syntax. Or to provide specific descriptive names to a type.

How to use typedef?

We use typedef keyword, to define an alias or synonym for an existing type.

Syntax

typedef <existing-type> <new-type-identifier>;

Example

typedef int Integer;

The above statement declare an alias for int with name Integer. Which means the following two statements are equivalent in meaning.

int     num1 = 0;   // Declare a variable of int type
Integer num2 = 0;   // Declare a variable of int type

Similarly you can define typedef to any existing type. Let us take another example and learn how to define typedef for structures or unions.

Example

struct point {
    int x;
    int y;
};

// Declare typedef for point structure
typedef struct point Point;

You can also define typedef along with structure definition.
Example

typedef struct point {
    int x;
    int y;
} Point;

When to use typedef in C language?

typedef provides an alias name to the existing complex type definition. With typedef you can simply create alias for any type. Whether it is a simple integer to complex function pointer or structure declaration, typedef will shorten your code.

You will use typedef most of the cases for creating alias for complex types. It makes more sense and keeps your code more readable. It is always better to create a simple alias for a complex type declaration, rather using a complex type declaration everywhere in the code.

For example

int (* compare1)(const void *, const void *);
int (* compare2)(const void *, const void *);

For the above declaration you can create a typedef to simplify its declaration as

// Define alias
typedef int (* Fptr)(const void *, const void *);

// Declare variable for the alias
Fptr compare1;
Fptr compare2;

You can also use typedef to create alias for predefined types. But, it is not recommended since it will make your code more obfuscate.

Program to demonstrate typedef in C for various types

In the below program I will create alias for different C predefined and custom types.

/**
 * C program to demonstrate typedef declaration and use.
 */

#include <stdio.h>

// Typedef to a structure
typedef struct point {
    int x;
    int y;
} Point;


// Tyepdef to a function pointer accepting two void pointers 
// and returing integer
typedef int (* Fptr)(const void *, const void *);


int main()
{
    // Typedef to primitive types
    typedef int Integer;

    // Typedef to a character array
    typedef char CharArray[100];

    // Creating variables for all typedef's
    Fptr functionPointer;
    
    // Use Point typedef
    Point point = { 10, 20 };

    // Use integer typedef
    Integer num = 100;

    // Using char array typedef
    CharArray name = "Codeforwin";


    // Print all values
    printf("Point x=%d, y=%d\n", point.x, point.y);
    printf("num = %d\n", num);
    printf("name = %s\n", name);
    
    return 0;
}

There is always a misconception in the mind of beginners regarding typedef, that we can only define it outside main method. But, you can define it anywhere in your program as in the above program I did.

Output

Point x=10, y=20
num = 100
name = Codeforwin

Happy Coding 😉

About Pankaj

Blogger, Programmer, developer, music lover, and learner. I started this blog to share a bit of knowledge what I have to the world.

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>