C 語言typedef 和 enum
typedef 給型別起別名,比如 typedef struct { … } Student;;enum 定義一組有名字的整數常量,常和 switch 一起用。
前兩課每次用結構體都要寫 struct student,有點囉嗦。typedef 能給一個型別起個新名字,寫起來短一些:
1#include <stdio.h>2 3typedef struct {4 char name[20];5 int score;6} Student;7 8int main(void) {9 Student s = { "小林", 95 };10 Student t = { "小周", 88 };11 printf("%s:%d\n", s.name, s.score);12 printf("%s:%d\n", t.name, t.score);13 return 0;14}
小林:95 小周:88
Student 就是這個結構體的名字,前面不用再寫 struct。這一課講 typedef,還有另一樣常和它一起用的東西:enum。
有了 typedef struct { ... } Student;,怎麼宣告一個變數?
typedef 給這個結構體起的名字就是 Student,直接拿它當型別用。這個結構體花括號前面沒寫名字,所以沒有 struct Student 這種寫法。typedef 起別名
typedef 的寫法是:typedef 原來的型別 新名字;。它不造出新的型別,只是給原來的型別多起一個名字,兩個名字用起來完全一樣:
1#include <stdio.h>2 3typedef int Minutes;4 5Minutes total(Minutes a, Minutes b) {6 return a + b;7}8 9int main(void) {10 Minutes run = 25;11 Minutes walk = 40;12 printf("一共 %d 分鐘\n", total(run, walk));13 return 0;14}
一共 65 分鐘
Minutes 就是 int,顯示時照樣用 %d。好處是讀程式碼的人一看就知道這個數是什麼:是分鐘,不是隨便一個整數。你早就用過一個這樣的名字:strlen 返回的 size_t,就是標準庫用 typedef 起的名字,背後是一種不帶負數的整數。
typedef int Minutes; 之後,Minutes m = 5; 裡的 m 是什麼型別?
typedef 不造新型別,只是起別名。Minutes 和 int 完全一樣:能和別的 int 一起算,顯示也用 %d。typedef struct
最常見的用法是給結構體起名字:起好以後寫 Point 就行,不用每次都寫 struct point。可以先定義結構體,再單獨寫一行 typedef:
1#include <stdio.h>2 3struct point {4 int x;5 int y;6};7 8typedef struct point Point;9 10int main(void) {11 Point a = { 3, 4 };12 struct point b = { 1, 2 };13 printf("(%d, %d) 和 (%d, %d)\n", a.x, a.y, b.x, b.y);14 return 0;15}
(3, 4) 和 (1, 2)
Point 和 struct point 是同一個型別,兩種寫法可以混著用。所以也常見這樣一次寫完,兩個名字都有:
typedef struct point① { int x; int y;} Point②;
- ①結構體本來的名字:struct point
- ②新起的短名字:直接寫 Point
花括號前面的 point 也可以不寫,寫成 typedef struct { ... } Point;,那就只有 Point 這一個名字。什麼時候必須寫?結構體裡要用到它自己的時候,比如鏈結串列的節點要指向下一個節點。下一課就會用到。
長方形
✓用 typedef struct 定義 Rect,成員是 width 和 height(都是 int)。寫函式 area,引數是一個 Rect,返回面積。程式要顯示 面積:24。
面積:24
1#include <stdio.h>2 3// 在這裡定義 Rect 和 area4 5int main(void) {6 Rect r = { 4, 6 };7 printf("面積:%d\n", area(r));8 return 0;9}
enum:有名字的整數
一個訂單的狀態只有幾種:待付款、已付款、已發貨、已送達。可以用 0、1、2、3 表示,可過幾天再看程式碼,status == 2 是什麼意思就忘了。列舉(enum)給這幾個數各起一個名字:
1#include <stdio.h>2 3enum status { PENDING, PAID, SHIPPED, DELIVERED };4 5int main(void) {6 enum status s = SHIPPED;7 printf("PENDING = %d\n", PENDING);8 printf("DELIVERED = %d\n", DELIVERED);9 printf("s = %d\n", s);10 if (s == SHIPPED) {11 printf("已經發貨了\n");12 }13 return 0;14}
PENDING = 0 DELIVERED = 3 s = 2 已經發貨了
不寫數的話,第一個名字是 0,後面一個比一個大 1。這些名字其實就是 int 常量:能比較、能顯示成 %d。習慣上全用大寫字母,一看就知道是常量。
這段程式顯示什麼?
1#include <stdio.h>2 3enum color { RED, GREEN, BLUE };4 5int main(void) {6 printf("%d %d\n", RED, BLUE);7 return 0;8}
RED 是 0,GREEN 是 1,BLUE 是 2。自己定數
也可以給名字指定數。指定了的就用指定的,後面沒指定的接著加 1。比如讓星期一是 1:
1#include <stdio.h>2 3enum weekday { MON = 1, TUE, WED, THU, FRI, SAT, SUN };4 5int main(void) {6 enum weekday today = WED;7 printf("今天是一週的第 %d 天\n", today);8 printf("離週日還有 %d 天\n", SUN - today);9 return 0;10}
今天是一週的第 3 天 離週日還有 4 天
這段程式顯示什麼?
1#include <stdio.h>2 3enum level { LOW = 1, MIDDLE, HIGH = 10, TOP };4 5int main(void) {6 printf("%d %d\n", MIDDLE, TOP);7 return 0;8}
LOW 指定了 1,MIDDLE 沒指定,接著加 1 就是 2。HIGH 指定了 10,TOP 接著它加 1,是 11。向右轉
✓方向按順時針排:北、東、南、西。寫函式 turn_right,返回向右轉之後的方向:北變東,……,西又變回北。程式從北開始轉 5 次,每次顯示新的方向,最後一行是 東。
東 南 西 北 東
1#include <stdio.h>2 3typedef enum { NORTH, EAST, SOUTH, WEST } Direction;4 5Direction turn_right(Direction d) {6 7}8 9int main(void) {10 char names[4][8] = { "北", "東", "南", "西" };11 Direction d = NORTH;12 for (int i = 0; i < 5; i++) {13 d = turn_right(d);14 printf("%s\n", names[d]);15 }16 return 0;17}
switch 配 enum
enum 和 switch 是天生一對:每種情況一個 case,名字比數清楚多了。這一關的例子裡,函式 show_status 把訂單狀態變成一段文字:
1#include <stdio.h>2 3typedef enum { PENDING, PAID, SHIPPED, DELIVERED } Status;4 5void show_status(Status s) {6 switch (s) {7 case PENDING:8 printf("待付款\n");9 break;10 case PAID:11 printf("已付款\n");12 break;13 case SHIPPED:14 printf("已發貨\n");15 break;16 case DELIVERED:17 printf("已送達\n");18 break;19 }20}21 22int main(void) {23 show_status(PAID);24 show_status(DELIVERED);25 return 0;26}
已付款 已送達
typedef enum { ... } Status; 和 typedef struct 一樣,給列舉起了個短名字,之後寫 Status 就行。別忘了每個 case 後面的 break(第 5 課)。
杯子的大小
✓寫函式 price,引數是一個 Size,用 switch 返回價格:SMALL 是 12,MEDIUM 是 15,LARGE 是 18。程式要顯示 12 15 18。
12 15 18
1#include <stdio.h>2 3typedef enum { SMALL, MEDIUM, LARGE } Size;4 5int price(Size s) {6 7}8 9int main(void) {10 printf("%d %d %d\n", price(SMALL), price(MEDIUM), price(LARGE));11 return 0;12}
放在一起用
結構體裡可以有一個列舉成員。一組訂單,每張有編號和狀態,數一數還有幾張沒送到:
1#include <stdio.h>2 3typedef enum { PENDING, PAID, SHIPPED, DELIVERED } Status;4 5typedef struct {6 int id;7 Status status;8} Order;9 10int main(void) {11 Order orders[4] = { { 1001, DELIVERED }, { 1002, SHIPPED }, { 1003, PAID }, { 1004, DELIVERED } };12 int waiting = 0;13 for (int i = 0; i < 4; i++) {14 if (orders[i].status != DELIVERED) {15 printf("訂單 %d 還沒送到\n", orders[i].id);16 waiting++;17 }18 }19 printf("一共 %d 張\n", waiting);20 return 0;21}
訂單 1002 還沒送到 訂單 1003 還沒送到 一共 2 張
點單步看看:每個 Order 是一個框,status 那一格裡放的就是一個整數。
小技巧: C 不會檢查列舉變數裡放的是不是列出來的那幾個數:Status s = 7; 照樣能編譯。所以 switch 裡最好加一個 default,碰到意外的數也有個交代。
這段程式顯示什麼?
1#include <stdio.h>2 3typedef enum { OFF, ON } State;4 5typedef struct {6 int id;7 State state;8} Lamp;9 10int main(void) {11 Lamp lamps[3] = { { 1, ON }, { 2, OFF }, { 3, ON } };12 int lit = 0;13 for (int i = 0; i < 3; i++) {14 if (lamps[i].state == ON) {15 lit++;16 }17 }18 printf("%d %d\n", lit, lamps[1].state);19 return 0;20}
lit 是 2。lamps[1].state 是 OFF:列舉的名字就是整數,用 %d 顯示出來是 0,不會變成文字 OFF。這一課學會了
typedef 原來的型別 新名字;給型別起別名,不造新型別;size_t就是這樣來的。typedef struct { ... } Student;之後寫Student就行,不用再寫struct;結構體要用到自己時,花括號前面要寫名字。enum給一組整數起名字:預設從0開始,一個比一個大 1;也可以自己指定,後面的接著加 1。- 列舉的名字就是
int常量,能比較、能用%d顯示,習慣上全大寫。 switch配enum,每種情況一個case;C 不檢查列舉裡放的數,最好加default。