C typedef keyword

C Typedef Keyword Explained By Examples

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello readers, welcome to our blog. In the last article, you had learnt about the Union In C Programming Language. I hope you all understood how to use a union in c, how to define union and how to access union member functions but 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 name of a data type. With this keyword, you can give new types 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 data types in the program is complex, then this keyword can define its simple name. 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 create a variable of structure, you will do the same way.

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

The syntax of C typedef Keyword

The general syntax for 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 declared, which you want to change. At last, the new name is written which you want to use.

Example of C typedef Keyword

The use of typedef keywords in C language is being explained by the following example.
#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 salary is 10000
I hope you understand how to use typedef keyword in C Programming Language. If you understand then please share my hard work with your friends. Subscribe my newsletter for upcoming article’s updates direct in your mailbox. If you have any confusion or query then you can leave a message on my facebook page. At last, I would like to say please don’t forget to subscribe to my youtube channel. Thank you for visiting.

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

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