typedef in c programming
C programming Tutorials

Typedef in C Programming

What is Typedef in C Programming ?

  • Typedef in C is an important keyword or reserved word.
  • Typedef is used to create an additional name (alias) for a data type. This is important to note that typedef does not create another data type.
  • Typedef is a keyword using which the user can give a new name to an already existing data type.
  • Typedef gives freedom to the user by allowing them to create their own data types.

Frequently Asked Questions

  • What do you mean by typedef in C ?
  • Should I use typedef in C ?
  • What is the use of typedef in C ?
  • What is typdef enum ?
  • How do enums work ?

Syntax of Typedef in C

The syntax of using typedef keyword is given below

typedef existing_data_type new_data_type.

Example of Typedef in C

We can understand the use of typedef in c programming with an example . Consider the program given below –

Example 1

#include <stdio.h>

typedef int INTEGER; //renaming of int data type by

INTEGERint main() {

INTEGER var=100;

printf(“%d”,var);

return 0;

}

Output

100

 

I the above program INTEGER is assigned as an alias for int data type so after this we can use INTEGER in place of int in the program.

Another example of typedef in c is given below –

Example 2

Another program for use of typedef in C programming is given below

#include <stdio.h>

typedef struct student {

//old data type struct

int mark;

char name[50];

char school_name[50];

int age;

}student;   //new data type student

int main(){

student s1;  //new data type created by the user

}

 

typedef keyword helps the user define their own data types.

The new name given to the data type can be called as alias name.

Use of Typedef in Structure

We can also use typed in a Structure in C program. The example of typedef in Structure is given below –

#include <stdio.h>

typedef struct student{

//old data type struct

int mark;

char name[5];

char school_name;

int age;

}student;   //new data type student

int main(){

student s1,s2;//new data type created by the user

s1.mark=87;

s2.mark=67;

printf(“marks obtained by s1 %d\n”,s1.mark);

printf(“marks obtained by s2 %d”,s2.mark);

Output

marks obtained by s1 87

marks obtained by s2 67

Use of Typedef with Pointers

We can also use the typedef  keyword in Pointer in C Programming.

Program for use of typedef keyword with pointer is give below.

#include <stdio.h>

int main(){

typedef int* intpointer;

intpointer a,b;

int c=89;

a=&c;

b=&c;

printf(“%d”,c);

}

Output

89

 

Conclusion and Summary

  • In this tutorial we have discussed the use of typedef in c Programmig with program
  • Use of typedef  with pointers in C is also discussed.
  • I hope after reading this tutorial students will be able to answer the questions related to typedef keyword in c.

Leave a Reply

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