首頁  >  文章  >  後端開發  >  使用C語言中的typedef關鍵字來解釋結構體

使用C語言中的typedef關鍵字來解釋結構體

王林
王林轉載
2023-08-25 13:25:151237瀏覽

使用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&rdquo;);
   scanf ("%d&rdquo;, &h);
   printf("Minutes =%d&rdquo;, h*60);
   printf("Seconds = %d&rdquo;, 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&rdquo;, 5000};
   clrscr();
   printf("number = %d&rdquo;, e.eno);
   printf("name = %d&rdquo;, e.ename);
   printf("salary = %d&rdquo;, e.sal);
   getch ();
}

輸出

Number=10
Name=ramu
Salary=5000

以上是使用C語言中的typedef關鍵字來解釋結構體的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除