Table of Contents

    Understanding typedef in C: Simplifying Complex Declarations

    Understanding typedef in C: Simplifying Complex Declarations

    The C programming language provides a keyword called typedef, which you can use to give a type a new name. OR WE CAN SAY, Typedef is a keyword that is used to give a new symbolic name for the existing name in a C program. This is same as defining alias for the commands. Following is an example to define a term MAN for one-byte numbers ?

    typedef unsigned int MAN;

    After this type definition, the identifier MAN can be used as an abbreviation for the type unsigned char, for example.

    MAN  a1, a2;

    By convention, uppercase letters are used for these definitions to remind the user that the type name is really a symbolic abbreviation, but you can use lowercase, as follows ?

    typedef unsigned int man;

    Example Program:

    
     #include<stdio.h>
    void main(){ 
    	typedef unsigned int MAN;
    	MAN a1, a2 ;
    	a1 = 12;
    	a2 = 13;
    	
    	printf("%u \n",a1);
    	printf("%u \n",a2);
    }
     

    Output:

    12
    13
    Press any key to continue . . .

    typedef : typedef is used to give data type a new name, for example

    Another Example Program:

    
    // C program to demonstrate typedef
    #include <stdio.h>
     
    // After this line BYTE can be used
    // in place of unsigned char
    typedef unsigned char BYTE;
     
    int main()
    {
        BYTE b1, b2;
        b1 = 'c';
        printf("%c ", b1);
        return 0;
    }
     

    Output:

    c Press any key to continue . . .

    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.

    Another Example Program:

    
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
       typedef long long int LLI ;
       LLI  a = 768783793;
       printf("Storage size for long long int data type : %ld \n", sizeof(LLI));
       printf(" %ld  \n",a);
    
       return 0;
    }
     

    Output:

    Storage size for long long int data type : 8
     768783793
    Press any key to continue . . .

    typedef in structure

    You can use typedef to give a name to your user-defined data types as well. For example, you can use typedef with a structure to define a new data type and then use that data type to define structure variables directly as follows ?

    Consider the below structure.

    struct student
    {
        int marks[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 Program:

    
    // 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, "atnyla");
      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: atnyla
     Percentage is: 86.500000
    Press any key to continue . . .

    Another Example Program:

    
    #include <stdio.h>
    #include <string.h>
     
    typedef struct Books {
       char title[50];
       char author[50];
       char subject[100];
       int book_id;
    } Book;
     
    int main( ) {
    
       Book book;
     
       strcpy( book.title, "ANSCII C");
       strcpy( book.author, "Dennis sir"); 
       strcpy( book.subject, "C Programming Tutorial");
       book.book_id = 6495407;
     
       printf( "Book title : %s\n", book.title);
       printf( "Book author : %s\n", book.author);
       printf( "Book subject : %s\n", book.subject);
       printf( "Book book_id : %d\n", book.book_id);
    
       return 0;
    }
     

    Output:

    Book title : ANSCII C
    Book author : Dennis sir
    Book subject : C Programming Tutorial
    Book book_id : 6495407
    Press any key to continue . . .