[C语言] C语言 枚举简单用法

[复制链接]
 楼主| 一路向北lm 发表于 2020-3-20 11:06 | 显示全部楼层 |阅读模式
写了一个枚举的简答测试例子:
  1. #include<stdio.h>

  2. typedef enum
  3. {
  4.   LED1 = 1,
  5.   LED2 = 2,
  6.   LED3 = 3
  7. }led_TypeDef;

  8. //LED点亮函数
  9. void LED_ON(led_TypeDef led)
  10. {
  11.         switch(led)
  12.         {
  13.                 case LED1:
  14.                         printf("this is LED1 is on\r\n");
  15.                 return;
  16.                 case LED2:
  17.                         printf("this is LED2 is on\r\n");
  18.                 return;
  19.                 case LED3:
  20.                         printf("this is LED3 is on\r\n");
  21.                 return;
  22.         }
  23. }

  24. //LED熄灭函数
  25. void LED_OFF(led_TypeDef led)
  26. {
  27.         switch(led)
  28.         {
  29.                 case LED1:
  30.                         printf("this is LED1 is off\r\n");
  31.                 return;
  32.                 case LED2:
  33.                         printf("this is LED2 is off\r\n");
  34.                 return;
  35.                 case LED3:
  36.                         printf("this is LED3 is off\r\n");
  37.                 return;
  38.         }
  39. }

  40. int main(int argc,char*argv[])
  41. {
  42.   LED_ON(LED2);
  43.   while(1);
  44. }


airwill 发表于 2020-3-22 12:17 | 显示全部楼层
枚举就相当于数据另外起个别名罢了
 楼主| 一路向北lm 发表于 2020-3-22 12:53 | 显示全部楼层
airwill 发表于 2020-3-22 12:17
枚举就相当于数据另外起个别名罢了

是啊,不用写这么多宏了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部