使用C语言中的typedef关键字来解释结构体

王林
王林 转载
2023-08-25 13:25:15 862浏览

使用C语言中的typedef关键字来解释结构体

Typedef

‘C’允许使用‘typedef’关键字定义新的数据类型名称。使用‘typedef’,我们不能创建新的数据类型,而是为已经存在的类型定义一个新的名称。

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 ();
}

输出

Enter hours =1
Minutes = 60
Seconds = 360

typedefining一个结构的示例

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 ();
}

输出

Number=10
Name=ramu
Salary=5000

以上就是使用C语言中的typedef关键字来解释结构体的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除