C Typedef Keyword Explained By Examples

C Typedef Keyword Explained By Examples

Hello readers, welcome to our blog. In the last article, you had learned about the Union In C Programming Language. I hope you all understood how to use a union in c, define association, and access union member functions. Still, in today’s article, you will learn how to use a c typedef keyword.  So let’s start.

Introduction to C typedef Keyword

Typedef is a keyword in C language. This is used to give a new name to the current term of a data type. With this keyword, you can provide new kinds of data types, both built-in and user-defined.

This keyword is used mainly with user-defined data types such as structure etc. When the name of the program’s data types is involved, this keyword can define its simple word. Using this keyword, the code decreases, and the readability of the program increases.

For example, look at the given structure below.

struct Employee
{
char Name[20];
int Age;
};

You can create variables of the structure above here.

struct Employee e1;

The struct Employee has also been announced to create e1 in the declaration given above. Whenever you make a variable of structure, you will do the same way.

But using the c typedef keyword, you can make this declaration a short and straightforward word. By doing so, you can use the same name every time.

The syntax of C typedef Keyword

The general syntax for the typedef keyword is being given below in C language.

typedef-keyword data-type-name new-name;

As you can see in the syntax given above, the first typedef is declared. Then the name of that type is displayed, which you want to change. At last, the new name is written which you wish to use.

Example of C typedef Keyword

The following example is explaining the use of typedef keywords in C language.

The struct Employee has also been announced to create e1 in the declaration given above. Whenever you make a variable of structure, you will do the same way.

But using the c typedef keyword, you can make this declaration a short and straightforward word. By doing so, you can use the same name every time.

The syntax of C typedef Keyword

The general syntax for the typedef keyword is being given below in C language.

typedef-keyword data-type-name new-name;

As you can see in the syntax given above, the first typedef is declared. Then the name of that type is displayed, which you want to change. At last, the new name is written which you wish to use.

Example of C typedef Keyword

The following example is explaining the use of typedef keywords in C language.

#include<stdio.h>

struct Employee
{
char Id;
int Salary;
};

int main()
{
typedef struct Employee emp;emp e1;e1.Id=101;
e1.Salary=10000;printf(“Employee Id is %d and salary is %d\n”,e1.Id,e1.Salary);

return 0;
}

In the example given above, the struct Employee declaration by typedef has been converted to emp. This example produces the below-given output.

Employee Id is 101, and the salary is 10000

I hope you understand how to use the typedef keyword in C Programming Language. If you know, then please share my hard work with your friends. Subscribe to my newsletter for upcoming article updates directly in your mailbox.

Leave a Reply

Your email address will not be published.