枚舉是C語言中的使用者定義資料型別。它用於給整數常數賦予名稱,使程式易於閱讀和維護。關鍵字“enum”用於聲明一個枚舉。
以下是C語言中枚舉的語法:
enum enum_name{const1, const2, ....... };
The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows .
enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday}; enum week day;
Here is an example of enum in C language,
#include<stdio.h> enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun}; enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund}; int main() { printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue, Wed, Thur, Fri, Sat, Sun); printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond , Tues, Wedn, Thurs, Frid, Satu, Sund); return 0; }
The value of enum week: 10111213101617 The default value of enum day: 0123181112
以上是如何在C/C++中使用枚舉?的詳細內容。更多資訊請關注PHP中文網其他相關文章!