Friday, 3 April 2020

Typedef in C with examples


Consider following example:
#include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };

int main(){
   struct student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   struct student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

As you can see in above example, every time, a structure variable is being declared, we have to write struct student varName.
To avoid writing struct all over the place, we can use typedef with structure declaration. Using a typedef avoids having to write struct every time you declare a variable of that type.
 Example:
#include<stdio.h>
   typedef struct student{
       int rollno;
       float marks;
   };
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of above program:
  #include<stdio.h>
   typedef struct {
       int rollno;
       float marks;
   }student;
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of same program:
  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };
   typedef struct student;
int main(){
   student s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   student s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Another version of above program:
  #include<stdio.h>
   struct student{
       int rollno;
       float marks;
   };
   typedef struct student s;
int main(){
   s s1;
   s1.rollno=12;
   s1.marks=92.5;
  
   printf("\nStudent 1 Details\n");
   printf("\nRoll No:%d",s1.rollno);
   printf("\nMarks:%.2f\n",s1.marks);
  
   s s2;
   s2.rollno=13;
   s2.marks=90;
  
   printf("\nStudent 2 Details\n");
   printf("\nRoll No:%d",s2.rollno);
   printf("\nMarks:%.2f\n",s2.marks);

 return 0;
}

Summary:
(i). Typedef keyword is used to give a new symbolic name(i.e alias) to another existing type.
(ii). It saves some typing.
(iii). It makes code cleaner.




Please comment and share if you find it useful.

No comments:

Post a Comment