枚举__2018.06.17

本文通过一个C语言示例介绍了枚举类型的定义与使用方法。枚举类型是一种将一组命名常量集合定义为一种数据类型的方式,它使得代码更具可读性。文章中还对比了枚举类型与基本数据类型的变量声明及赋值过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么会有枚举这种数据类型呢?

代码:

#include<stdio.h>

/* 定义枚举类型 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };

void main()
{
    /* 使用基本数据类型声明变量,然后对变量赋值 */
    int x, y, z;
    
    x = 10;
    y = 20;
    z = 30;
    
    /* 使用枚举类型声明变量,再对枚举型变量赋值 */
    enum DAY yesterday, today, tomorrow;
    
    yesterday = MON;
    today     = TUE;
    tomorrow  = WED;

    printf("%d %d %d \n", yesterday, today, tomorrow);
}


*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'D:\Kile5\Core\ARM\ARMCC\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... compiling core_cm3.c... compiling stm32f10x_dbgmcu.c... compiling misc.c... compiling stm32f10x_bkp.c... compiling system_stm32f10x.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_can.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_dma.c... compiling stm32f10x_exti.c... compiling stm32f10x_flash.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_iwdg.c... compiling stm32f10x_gpio.c... compiling stm32f10x_pwr.c... compiling stm32f10x_rtc.c... compiling stm32f10x_i2c.c... compiling stm32f10x_rcc.c... compiling stm32f10x_sdio.c... compiling stm32f10x_spi.c... compiling stm32f10x_usart.c... compiling stm32f10x_wwdg.c... compiling stm32f10x_tim.c... compiling Key.c... Hardware\Key.c(12): error: #101: "KEY_EVENT_NONE" has already been declared in the current scope KEY_EVENT_NONE, Hardware\Key.c(13): error: #101: "KEY_EVENT_CLICK" has already been declared in the current scope KEY_EVENT_CLICK, Hardware\Key.c(61): error: #20: identifier "key_event" is undefined key_event = KEY_EVENT_LONG_PRESS; Hardware\Key.c(79): error: #20: identifier "key_event" is undefined key_event = KEY_EVENT_DOUBLE_CLICK; Hardware\Key.c: 0 warnings, 4 errors compiling Delay.c... compiling LEDSEG.c... Hardware\LEDSEG.c(25): warning: #188-D: enumerated type mixed with another type GPIO_WriteBit( PIOA,GPIO_PIN_x[i],display_val>>i & 0x01); Hardware\LEDSEG.c: 1 warning, 0 errors compiling robot.c... compiling PWM.c... compiling Incontrol.c... compiling Irobstacle.c... compiling Serial.c... compiling timer.c... compiling UltrasonicWave.c... compiling Irtracking.c... compiling Servo.c... compiling Buzzer.c... compiling ServoPWM.c... compiling sys.c... compiling beep.c... compiling IR.c... compiling Uart3.c... compiling ma
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值