C Typedef

typedef is a keyword used in C language to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated. Generally typedef are use to create an alias name (nickname).

Following is the general syntax for using typedef,

typedef existing_name alias_name

Lets take an example and see how typedef actually works.

Consider the below structure.

struct student
{
         int mark [2];
         char name [10];
         float average;
}


Variable for the above structure can be declared in two ways.

1st way :

struct student record;       /* for normal variable */
struct student *record;     /* for pointer variable */

2nd way :

typedef struct student status;

When we use “typedef” keyword before struct like above, after that we can simply use type definition “status” in the C program to declare structure variable.

Now, structure variable declaration will be, “status record”.

This is equal to “struct student record”. Type definition for “struct student” is status. i.e. status = “struct student”

AN ALTERNATIVE WAY FOR STRUCTURE DECLARATION USING TYPEDEF IN C:

typedef struct student
{
         int mark [2];
         char name [10];
         float average;
} status;

To declare structure variable, we can use the below statements.

status record1;                 /* record 1 is structure variable */
status record2;                 /* record 2 is structure variable */


Example 1: Program for C Typedef

// Structure using typedef:
 
#include<stdio.h>
#include<string.h>

 
typedef struct student 
{
  int id;
  char name[20];
  float percentage;
} status;
 
int main() 
{
  status record;
  record.id=1;
  strcpy(record.name, "Surabhi Sahu");
  record.percentage = 86.5;
  printf(" Id is: %d \n", record.id);
  printf(" Name is: %s \n", record.name);
  printf(" Percentage is: %f \n", record.percentage);
  return 0;
}

Output

Id is: 1
Name is: Surabhi Sahu
Percentage is: 86.500000


MIND IT !

Typedef can be used to simplify the real commands as per our need.

For example, consider below statement.

typedef long long int LLI;

In above statement, LLI is the type definition for the real C command “long long int”. We can use type definition LLI instead of using full command “long long int” in a C program once it is defined.


Example 2: Program for C Typedef

#include<stdio.h>
#include<limits.h>

int main()
{
   typedef long long int LLI;
 
   printf("Storage size for long long int data " \
          "type  : %ld \n", sizeof(LLI));
 
   return 0;
}

Output

Storage size for long long int data type : 8

Application of typedef

typedef can be used to give a name to user defined data type as well. Lets see its use with structures.

typedef struct
{
  type member1;
  type member2;
  type member3;
} type_name ;

Here type_name represents the structure definition associated with it. Now this type_name can be used to declare a variable of this structure type.

type_name t1, t2 ;


Example of structure definition using typedef

#include<stdio.h>
#include<conio.h>
#include<string.h>

 
typedef struct employee
{
 char  name[50];
 int   salary;
} emp ;
 
void main( )
{
 emp e1;
 printf("\nEnter Employee record\n");
 printf("\nEmployee name\t");
 scanf("%s",e1.name);
 printf("\nEnter Employee salary \t");
 scanf("%d",&e1.salary);
 printf("\nstudent name is %s",e1.name);
 printf("\nroll is %d",e1.salary);
 getch();
}

typedef and Pointers

typedef can be used to give an alias name to pointers also. Here we have a case in which use of typedef is beneficial during pointer declaration.

In Pointers * binds to the right and not the left.

int* x, y ;

By this declaration statement, we are actually declaring x as a pointer of type int, whereas y will be declared as a plain integer.

typedef int* IntPtr ;
IntPtr x, y, z;

But if we use typedef like in above example, we can declare any number of pointers in a single statement.

NOTE : If you do not have any prior knowledge of pointers, do study Pointers first.

LearnFrenzy provides you lots of fully solved "C Typedef" Questions and Answers with explanation.