enum 和 typedef, 在两个小节中书中分别出现了2个例子,如下
enum direction {north, south, east, west};
typedef enum {north, south, east, west} direction;
typedef enum {north, south, east, west} direction;
不禁产生疑问,这两个有什么区别,仔细对照了一下,发现是这样的:
大同小异,
同: 都是申明了一个枚举类型。
异:在使用该枚举类型定义变量的时候,语法不一样,举例如下:
1
enum direction {north, south, east, west};
2 enum direction facing = north;
1 typedef enum {north, south, east, west} direction;
2 direction facing = north;
2 enum direction facing = north;
1 typedef enum {north, south, east, west} direction;
2 direction facing = north;