Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

typedef in C

C is a powerful programming language with many features that allow programmers to write efficient and effective code. One such feature is the typedef keyword, which allows users to create new names for existing data types. This article will explain what typedef is and how it can be used in C.

What is typedef in C Language?

In C, typedef is a keyword that allows users to create aliases for existing data types. This means that instead of having to use the original data type name, users can use a new name that they have created using the typedef keyword. This can make code easier to read and understand, and can also help to make code more portable across different platforms.

Syntax for typedef in C

typedef  ;

Here, "original_type" is the name of the data type that you want to create an alias for, and "new_type_name" is the name that you want to give to the new alias.

Example of typedef in C

Let’s say that we want to create a new name for the data type "unsigned int". We can do this using the following code:

typedef unsigned int uint;

Now, instead of writing "unsigned int" every time we can declare a variable of this type using "uint".

For example,

unsigned int myVariable = 42; can be written as uint myVariable = 42; 

In the above example, we have seen the primary use of typedef. There are lots of uses of typedef. Let’s see all of them one by one.

Uses of typedef in C

  • Use of typedef Keyword with Simple Data Type
    In this, we’ll use typedef keyword with simple data type.

    #include <stdio.h>
    typedef unsigned int var;
    int main()  
        var x, y, z;
        x = 10;
        y = 20;
        z = x + y;
        printf("The sum of %u and %u is %u", x, y, z);
        return 0;


    The sum of 10 and 20 is 30

    Explanation: We are using the typedef keyword in the above code to replace the unsigned int with the ‘var.’ We can now use var instead of unsigned int anywhere in the code when required.

  • Uses of typedef Keyword with Structures
    One advantage of using typedef is to create custom data types. For example, we want to create a new data type called "myStruct" based on the "struct" data type. We can do this using the following code:

    #include <stdio.h>
    #include <string.h>
    typedef struct students 
      char name[50];
      char branch[50];
      int ID_no;
    } students;
    int main()
      students st;
             "Computer Science And Engineering");
      st.ID_no = 108;
      printf("Name: %s\n",;
      printf("Branch: %s\n", st.branch);
      printf("ID_no: %d\n", st.ID_no);
      return 0;


    Name: Kamlesh
    Branch: Computer Science And Engineering
    ID_no: 108
  • Use of typedef Keyword with Pointers

    #include <stdio.h>
    int main() {
        typedef int* ptr;
        ptr a;  // we can use ptr in place of int*
        int x;
        x = 50;
        a = &x;
        printf("The value of x is: %d\n", x);
        printf("The value of x by pointer is: %d", *a);
        return 0;


    The value of x is: 50
    The value of x by pointer is: 50

    Explanation: We are using the typedef keyword in the above code to replace the *‘int with the ‘ptr.’* We can now use ptr instead of ‘int‘ everywhere in the code when required. The name ptr itself suggests that it is a pointer variable. This increases the overall readability of the code.

Applications of typedef

  • The typedef keyword allows us to give a meaningful name to the existing data type which helps other users to understand the program more quickly.
  • The best thing about typedef when it is used with structure is that we don’t have to type struct again and again.
  • We can use typedef with arrays to declare any number of variables.

typedef vs # define

Sometimes it seems very confusing that typedef and #define are the same but they work differently and have their own use cases.

typedef #define
The interpretation of the typedef keyword is performed by the compiler. The interpretation of the #define statement is performed by the preprocessor.
The definition of typedef terminates with a semicolon. #define should not be terminated with a semicolon.
Using typedef we can only assign symbolic names to the types. #define can be used to define an alias for values as well, For e.g., you can define 1 as ONE, 3.14 as PI, etc.
The typedef keyword follows the scope rule, which states that if a new type is defined within a scope (within a function), the new type name will only be visible while the scope is present. In the case of #define, all the occurrences are replaced by the value when the preprocessor sees it.

Point-wise conclusion on typedef and #define

  • We can also define aliases for value as well by using #define. For example, you can define 3.14 as PI, etc whereas Typedef is limited to giving symbolic names to types only.
  • #define statements are interpreted by preprocessors, while typedef statements are interpreted by compilers.
  • There is no semicolon at the end of #define, but in the typedef, there is a semicolon at the end of it.

Program to show the use of #define

#include <stdio.h>
// macro definition
#define LIMIT 3
int main()
  for (int i = 0; i < LIMIT; i++) 
    printf("%d \n", i);
  return 0;



How Does typedef Work in C++?

  • In C++, typedef is a reserved keyword that allows programmers to create an alias for an existing data type. This can help simplify code by providing a higher level of abstraction from the underlying data types, allowing programmers to focus on coding concepts. Additionally, it can make it easier to write and clean up code using default methods such as destroy(), since garbage collectors are the primary region for deleting unnecessary code and freeing up memory space.
  • When using typedef, it is important to consider the sizes of the data types being modified, as this will affect the memory allocation required for variables. For example, large storage data type variables will require more memory space than small storage variables. However, by using typedef, programmers can more easily manage these complexities and write cleaner, more maintainable code.
  • The typedef keyword in C++ can be used to simplify the declaration of variables with compound types such as struct or union. Using structure pointers with typedef can also make it easier to declare multiple variables of the same type with single-level statements, regardless of whether the pointers are included in the structure type or not. This can help shorten and simplify programs, especially those that involve functional pointers.
  • By using typedef, it is possible to rewrite lengthy coding lines that specify functional pointers into more concise and readable code. This can make the functional declarations easier to understand, as they explicitly state the types of variables they will return and the types of inputs they will accept.
  • There are two standard typedef declarations in C++, which are typedef type-declaration and typedef type-name alias-name. These declarations can be used to create new names for existing types, making it easier to read and maintain code.

In conclusion, typedef is a useful feature of C that allows users to create new names for existing data types. One of the best things about typedef is that it allows coders to write code more quickly By using typedef, you can make your code more readable and easier to understand, as well as create custom data types that are based on existing data types. If you’re new to C, it’s worth taking the time to learn about typedef and how it can be used to improve your code.

FAQs related to typedef in C

1. What is typedef in C explain with example?
The typedef is a keyword that allows users to create aliases for existing data types.
Example of typedef in C

unsigned int myVariable = 42; can be written as: uint myVariable = 42; 

2. Is typedef a datatype?
The typedef is a predefined keyword. We can replace the name of the existing data type with the name which we have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays, etc.

3. What is the syntax of typedef?
The syntax of typedef is as follows:

typedef data_type new_name;
  • typedef: It is a keyword.
  • data_type: It is the name of any existing type or user-defined type created using structure/union.

4. What is the full form of typedef?
As the name suggests, typedef stands for "type definition." C++ typedef is simply a way of giving a new name to an existing data type. In other words, it is a reserved keyword we use to create an alias name for a specific data type. The function or purpose served by the data type remains unchanged.

Leave a Reply

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