用 C 語言中的 typedef 關鍵字解釋結構
Typedef
“C”允許使用“typedef”關鍵字定義新的資料型別名稱。使用“typedef”,我們不能建立新的資料型別,但可以為已經存在的資料型別定義一個新名稱。
語法
typedef datatype newname;
示例
typedef int bhanu; int a; bhanu a; %d
- 此語句告訴編譯器將“bhanu”識別為“int”的另一個名稱。
- “bhanu”用於建立另一個變數“a”。
- “bhanu a”將“a”宣告為型別為“int”的變數。
示例
#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
型別定義結構的示例
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
廣告