10. 计算下列程序输出
/*
* exercise19.c
*
* Created on: 2012-11-5
* Author: xiaobin
*/
enum myStr
{
x1,
x2,
x3=10,
x4,
x5
}x;
int main(int argc, char* argv[])
{
x = (enum myStr) 0x801005, 0x8010f4;
printf("%x\n", x + 5);
return 0;
}
首先,我们要知道mySt枚举常量的在未赋值的情况下,第一个常量为0,以此类推x2-1;在赋初值时,下一个常量+1,x4-11,x5-12;
然后,要知道枚举变量默认取第一个常量的值,即x1;
最后,要知道编译器对枚举变量不检查取值,即只取所赋的第一个值;
输出结果:
80100a
附:标准使用枚举的例子
enum status_type
{
ok = 200,
created = 201,
accepted = 202,
no_content = 204,
multiple_choices = 300,
moved_permanently = 301,
moved_temporarily = 302,
not_modified = 304,
bad_request = 400,
unauthorized = 401,
forbidden = 403,
not_found = 404,
internal_server_error = 500,
not_implemented = 501,
bad_gateway = 502,
service_unavailable = 503
} status;