一路向北lm 发表于 2020-3-20 11:06

C语言 枚举简单用法

写了一个枚举的简答测试例子:
#include<stdio.h>

typedef enum
{
LED1 = 1,
LED2 = 2,
LED3 = 3
}led_TypeDef;

//LED点亮函数
void LED_ON(led_TypeDef led)
{
        switch(led)
        {
                case LED1:
                        printf("this is LED1 is on\r\n");
                return;
                case LED2:
                        printf("this is LED2 is on\r\n");
                return;
                case LED3:
                        printf("this is LED3 is on\r\n");
                return;
        }
}

//LED熄灭函数
void LED_OFF(led_TypeDef led)
{
        switch(led)
        {
                case LED1:
                        printf("this is LED1 is off\r\n");
                return;
                case LED2:
                        printf("this is LED2 is off\r\n");
                return;
                case LED3:
                        printf("this is LED3 is off\r\n");
                return;
        }
}

int main(int argc,char*argv[])
{
LED_ON(LED2);
while(1);
}

airwill 发表于 2020-3-22 12:17

枚举就相当于数据另外起个别名罢了

一路向北lm 发表于 2020-3-22 12:53

airwill 发表于 2020-3-22 12:17
枚举就相当于数据另外起个别名罢了

是啊,不用写这么多宏了
页: [1]
查看完整版本: C语言 枚举简单用法