How to use Typedef Struct in C
The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable.
For better understanding, if you declare a variable using typedef function with int datatype, then for declaring another variable with integer data type we can use that user declared variable as datatype for the new created variable.
Syntax:
typedef <old_name or keyword> <new name for datatype>
Example:
typedef int NEW;
//In the above line we are declared a user defined variable “NEW” with data type int.
NEW a, b;
By using the user defined variable “NEW” we are declaring a and b variables. Here the NEW is acting like datatype to the variables a and b.
Here is the Example C program for typedef
#include<stdio.h>
#include<conio.h>
void main (void)
{
typedef int INTEGER;
INTEGER a=1 , b=2;
int c;
c = a +b;
printf(“Total=%d”,c);
}
Output:
3
In the above program, we are declared the variable “INTEGER” using typedef. By using the user defined variable INTEGER we are declaring another variable like a and b and printing the sum of them. The output of the program is 3.
Differences between #define and typedef.
#define:
The #define is used to assign the values / give an alias for the variables. This method is done in the preprocessor section. For example, we declare a pie as 3.14 using the #define then we can be able to use the pie values throughout out the program. In this we cannot be able to overwrite the value of the pie in program.
Example (program for #define):
#include<stdio.h>
#include<conio.h>
#define p 100
void main (void)
{
int a = 1, b;
b = p*a;
printf(b);
}
Output:
100
Typedef:
In the typedef we are declaring the variable with a datatype, then we can able to use that variable as the datatype for declaring another variable. The typedef is written inside the code and processed by the compiler.
Example (program for typedef):
//program for arrays using typedef
#include<stdio.h>
#include<conio.h>
void main(void)
{
typedef int array[5];
int a;
array i = {1, 2, 3, 4, 5};
for (a=0 ; a<5; a++)
{
printf(“Array Elements =%d\n”,i[a]);
}
}
Output:
Array Elements = 1
Array Elements = 2
Array Elements = 3
Array Elements = 4
Array Elements = 5
Using Typedef in Structures
The general structure declaration is given below
Example:
struct demo
{
int a;
};
struct demo d;
In the above example we have created a struct body and a variable d.To create a variable d we have to use another line, so to reduce the lines of code and complexity of code we use the typedef in structures.
Syntax
Syntax for Using typedef struct:
typedef struct
{
//statements
} variable;
Example (program for structures using typedef):
#include<stdio.h>
#include<conio.h>
void main (void)
{
typedef struct studentmarks
{
int rollno;
int marks;
}mk;
mk m1;
m1.rollno = 21;
m1.marks = 98;
printf(“the marks of %d is %d”, m1.rollno,m1.marks);
}
Output:
The marks of 21 is 98
Conclusion
The typedef keyword is used to declare a variable and use that variable as datatype for declaring another variable. The typedef in structures are used to reduce the lines of code and the complexity of the code for faster execution.