Home  >  Article  >  Backend Development  >  Use the typedef keyword in C language to interpret structures

Use the typedef keyword in C language to interpret structures

王林
王林forward
2023-08-25 13:25:151250browse

Use the typedef keyword in C language to interpret structures

Typedef

'C' allows the definition of new data type names using the 'typedef' keyword. Using ‘typedef’ we cannot create a new data type, but define a new name for an already existing type. The Chinese translation of

Syntax

typedef datatype newname;

Example

is:

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'.
  • The Chinese translation of

Example

is:

Example

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

Output

Enter hours =1
Minutes = 60
Seconds = 360

typedefining example of a structure

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

Output

Number=10
Name=ramu
Salary=5000

The above is the detailed content of Use the typedef keyword in C language to interpret structures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete