enum和typedef enum的区别

来源:互联网 发布:淘宝店铺外包合法吗 编辑:程序博客网 时间:2024/05/01 20:19

在了解enum和typedef enum的区别之前先应该明白typedef的用法和意义。

C语言里typedef的解释是用来声明新的类型名来代替已有的类姓名,例如:

typedef int   CHANGE;

指定了用CHANGE代表int类型,CHANGE代表int,那么:

int a,b;和CHANGE a,b;是等价的、一样的。

方便了个人习惯,熟悉的人用CHANGE来定义int。

typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。


而enum是枚举类型,有了typedef的理解容易看出,typedef  enum定义了枚举类型,类型变量取值在enum{}范围内取,在使用中二者无差别。

enum AlertTableSections

{

kUIAction_Simple_Section = 0,

kUIAction_OKCancel_Section,

kUIAction_Custom_Section,

kUIAlert_Simple_Section,

kUIAlert_OKCancel_Section,

kUIAlert_Custom_Section,

}; 


typedef enum {

    UIButtonTypeCustom = 0,           // no button type

    UIButtonTypeRoundedRect,          // rounded rect, flat white button, like in address card


    UIButtonTypeDetailDisclosure,

    UIButtonTypeInfoLight,

    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,

} UIButtonType;


看上面两个例子更好理解,下面的是UIButton的API,UIButtonType指定的按钮的类型,清楚名了,上面的直接调用enum里的元素就可以了。
0 0
原创粉丝点击