本書作者說:
Enums are a good idea that went bad.
好點子卻做壞事情。
他舉了一下帶來的好處,
#define NORTH 0
#define SOUTH 1
#define EAST 2
#define WEST 3
東西南北比0,1,2,3好記多了。這樣寫要4句話,用enum,
enum directions {NORTH, SOUTH, EAST, WEST}; 只要一句話。
參考一下例子,
http://www.java2s.com/Code/C/Data-Type/Mapenumtocharandoutputwithforloop.htm
#include <stdio.h>
enum spectrum { red, orange, yellow,purple,blue,pink} colour;
char *rainbow[] = { "red", "orange", "yellow" };
int main() {
for ( colour = red; colour <= yellow; colour++ ) {
printf ( "%s ", rainbow[colour]);
}
printf ( "\n" );
}
用open 這個system call時,用enum來表示多個屬性,O_WRONLY|O_TRUNC|O_CREAT|O_BINARY,
而fopen 這個標準函式庫,只能用char, 就比較不好表達,多個屬性,就要去查一下,"wb","r+"...