C語言 enum

這在篇blog中,我將介紹一些enum的用法。
(I will show some enum type examples in C.)

enum的基本語法 enum identifier { enumerator-list } 

1. enum裡的識別字,會以int的型態,從0開始排列,你也可以給于數值。

 2. enum也可以不宣告identifier , 如第二個enum的宣告方法, 可以減少define的使用,enumerator可以是相同的值,如範例中off跟yes都是1。
 (Different constants in an enumeration may have the same value, identifier is not necessary)


#include <stdio.h>

typedef enum {sun, mon, tue, wed, thu, fri, sat}week_type;
enum{on=0, off=1, no=0, yes=1};
main()
{
   week_type day;

   day=thu;

   printf("\nDay=%d\n",day);
   printf("%d %d %d %d\n", on, off, no, yes);
}

執結果(result):


Day=4
0 1 0 1

留言

這個網誌中的熱門文章

C語言,大數運算,階層筆記

Raspberry Pi (ARMv6)上自幹一個微小作業系統

Linux VLAN 筆記