可以用printf的%d來顯示,
預設的對應就是從數字0開始,
例如enum week{ Sun, Mon, Tue, Wed, Thu, Fri, Sat};
Sun = 0, Mon = 1, Tue = 2, Wed = 3..........................
當然這個對應可以手動指定,像是這樣:
enum week{ Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
手動指定之後後面若沒有手動指定則會依照手動指定的那一個一直遞增。
Sun = 1, Mon = 2, Tue = 3, Wed = 4.............
要注意有時候手動指定可能會有問題,像是這樣:
enum week{ Sun, Mon, Tue=1, Wed, Thu, Fri, Sat};
Sun = 0, Mon = 1, Tue = 1, Wed = 2.............
====================================================================
- 一般用法,宣告一個叫做week的列舉,宣告一個week型態的today且指明是哪一天
enum week today = Sun;
- 在宣告week時可以順便宣告這種型態的變數today
today = Sun;
- 可以使用typedef把enum week定義成Week型態,之後就直接使用Week型態來宣告變數即可,不必每次都再加入enum(簡單來說就是之後都用Week取代enum week)
Week today = Sun
沒有留言:
張貼留言