您好,欢迎访问一九零五行业门户网

如何在C/C++中使用枚举?

枚举是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,
example#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: 10111213101617the default value of enum day: 0123181112
以上就是如何在c/c++中使用枚举?的详细内容。
其它类似信息

推荐信息