C typedef

The typedef allows a programmer to define an identifier that would represent an existing data type. Therefore, the identifier so defined can be later used to declare variables. It is used to provide some meaningful names to the already existing variable in the C program.

Syntax
typedef data-type identifier;

In the above syntax, existing_name is the name of an already existing variable while identifier is another name given to the existing variable.

For example, suppose we want to create a variable of type unsigned int, then it becomes a tedious task if we want to declare multiple variables of this type. To overcome the problem, we use a typedef keyword.

snippet
typedef unsigned int unit;

In the above statements, we have declared the unit variable of type unsigned int by using a typedef keyword.

Now, we can create the variables of type unsigned int by writing the following statement:

snippet
unit a, b;

instead of writing

snippet
unsigned int a, b;

This typedef keyword is useful when dealing with the long data type especially, structure declarations.

Let's understand through a simple example.

snippet
#include <stdio.h>
int main()
{
typedef unsigned int unit;
unit i,j;
i=10;
j=20;
printf("Value of i is :%d",i);
printf("\nValue of j is :%d",j);
return 0;
}
snippet
Value of i is :10 
Value of j is :20

Using typedef with structures

Consider the below structure declaration

snippet
struct student
{
char name[20];
int age;
};
struct student s1;

In the above structure declaration, we have created the variable of student type by writing the following statement:

snippet
struct student s1;

The above statement is big to create a variable s1. We can use the typedef keyword to create the variable of type student.

snippet
struct student
{
char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;

In the above statement, as we declared the variable stud of type struct student, we can use the stud variable to create the variables of type struct student.

The above typedef can be written as

snippet
typedef struct student
{
char name[20];
int age; 
} stud;
stud s1,s2;

Let's see another example where we typedef the structure declaration.

snippet
#include <stdio.h>
typedef struct student
{
char name[20];
int age;
}stud;
int main()
{
stud s1;
printf("Enter the details of student s1: ");
printf("\nEnter the name of the student:");
scanf("%s",&s1.name);
printf("\nEnter the age of student:");
scanf("%d",&s1.age);
printf("\n Name of the student is : %s", s1.name);
printf("\n Age of the student is : %d", s1.age);
return 0;
}
snippet
Enter the details of student s1: 
Enter the name of the student: Peter 
Enter the age of student: 28 
Name of the student is : Peter 
Age of the student is : 28

Using typedef with pointers

Using typedef we can also provide another name or alias name to the pointer variables.

Consider below example to declare a pointer

snippet
int* ptr;

We can rename the above pointer variable as given below.

snippet
typedef int* ptr;

In the above statement, as we declared the variable stud of type int* we can use the ptr variable to create the variables of type int* using ptr.

snippet
ptr p1, p2 ;

In the above statement, p1 and p2 are the variables of type ptr.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +