设为首页 加入收藏

TOP

C语言枚举类型enum(二)
2013-09-24 09:20:15 来源: 作者: 【 】 浏览:1498
Tags:语言 枚举 类型 enum

 

  enum BOOLEAN { FALSE = 0, TRUE } match_flag;

  void main()

  {

  int index = 0;

  int count_of_letter = 0;

  int count_of_space = 0;

  char str[] = "I'm Ely efod";

  match_flag = FALSE;

  for(; str[index] != '/0'; index++)

  if( SPACE != str[index] )

  count_of_letter++;

  else

  {

  match_flag = (enum BOOLEAN) 1;

  count_of_space++;

  }

  printf("%s %d times %c", match_flag "match" : "not match", count_of_space, NEWLINE);

  printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);

  }

  输出:

  match 2 times

  count of letters: 10

  Press any key to continue

  4. 枚举类型与sizeof运算符

  #include <stdio.h>

  enum escapes

  {

  BELL      = '/a',

  BACKSPACE = '/b',

  HTAB      = '/t',

  RETURN    = '/r',

  NEWLINE   = '/n',

  VTAB      = '/v',

  SPACE     = ' '

  };

  enum BOOLEAN { FALSE = 0, TRUE } match_flag;

  void main()

  {

  printf("%d bytes /n", sizeof(enum escapes)); //4 bytes

  printf("%d bytes /n", sizeof(escapes)); //4 bytes

  printf("%d bytes /n", sizeof(enum BOOLEAN)); //4 bytes

  printf("%d bytes /n", sizeof(BOOLEAN)); //4 bytes

  printf("%d bytes /n", sizeof(match_flag)); //4 bytes

  printf("%d bytes /n", sizeof(SPACE)); //4 bytes

  printf("%d bytes /n", sizeof(NEWLINE)); //4 bytes

  printf("%d bytes /n", sizeof(FALSE)); //4 bytes

  printf("%d bytes /n", sizeof(0)); //4 bytes

  }

  5. 综合举例

  #include<stdio.h>

  enum Season

  {

  spring, summer=100, fall=96, winter

  };

  typedef enum

  {

  Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

  }

  Weekday;

  void main()

  {

  /* Season */

  printf("%d /n", spring); // 0

  printf("%d, %c /n", summer, summer); // 100, d

  printf("%d /n", fall+winter); // 193

  Season mySeason=winter;

  if(winter==mySeason)

  printf("mySeason is winter /n"); // mySeason is winter

  int x=100;

  if(x==summer)

  printf("x is equal to summer/n"); // x is equal to summer

  printf("%d bytes/n", sizeof(spring)); // 4 bytes

  /* Weekday */

  printf("sizeof Weekday is: %d /n", sizeof(Weekday)); //sizeof Weekday is: 4

  Weekday today = Saturday;

  Weekday tomorrow;

  if(today == Monday)

  tomorrow = Tuesday;

  else

  tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday

  }

        

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/22/22
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言实现定积分求解方法 下一篇C语言对象化编程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: