Explain structures using typedef keyword in C language

CServer Side ProgrammingProgramming

Typedef

‘C’ allows to define new datatype names using the ‘typedef’ keyword. Using ‘typedef’, we cannot create a new datatype but define a new name for already existing type.

Syntax

typedef datatype newname;

Example

typedef int bhanu;
int a;
bhanu a; %d
  • This statement tells the compiler to recognize ‘bhanu’ as another name for ‘int’.
  • ‘bhanu’ is used to create another variable ‘a’ .
  • ‘bhanu a ‘declares ‘a’ as a variable of type ‘int’.

Example

#include <stdio.h>
main (){
   typedef int hours;
   hours h; //int h;
   clrscr ();
   printf("Enter hours”);
   scanf ("%d”, &h);
   printf("Minutes =%d”, h*60);
   printf("Seconds = %d”, h*60*60);
   getch ();
}

Output

Enter hours =1
Minutes = 60
Seconds = 360

Example for typedefining a structure

typedef struct employee{
   int eno;
   char ename[30];
   float sal;
} emp;
main (){
   emp e = {10, "ramu”, 5000};
   clrscr();
   printf("number = %d”, e.eno);
   printf("name = %d”, e.ename);
   printf("salary = %d”, e.sal);
   getch ();
}

Output

Number=10
Name=ramu
Salary=5000
raja
Published on 09-Mar-2021 07:59:30
Advertisements