VS 2015认可的typedef enum定义和声明方式

调试一个程序,遇到报错:
Warning C4091 'typedef ': ignored on left of ‘XXX’ when no variable is declared XXX

参考博文:https://blog.youkuaiyun.com/weixin_37787043/article/details/79017817
其中的例子:

typedef enum
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
} workday; //此处的workday为枚举型enum workday的别名

workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,即enum workday

解决了该bug。

知识点:别名放到{}的后面,;的前面。

/* mbed Microcontroller Library ******************************************************************************* * Copyright (c) 2014, STMicroelectronics * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ #ifndef MBED_PERIPHERALNAMES_H #define MBED_PERIPHERALNAMES_H #include "cmsis.h" #ifdef __cplusplus extern "C" { #endif typedef enum { ADC_1 = (int)ADC1_BASE, ADC_2 = (int)ADC2_BASE, ADC_3 = (int)ADC3_BASE } ADCName; typedef enum { DAC_1 = (int)DAC_BASE } DACName; typedef enum { UART_1 = (int)USART1_BASE, UART_2 = (int)USART2_BASE, UART_3 = (int)USART3_BASE, UART_4 = (int)UART4_BASE, UART_5 = (int)UART5_BASE, UART_6 = (int)USART6_BASE } UARTName; #define STDIO_UART_TX PA_2 #define STDIO_UART_RX PA_3 #define STDIO_UART UART_2 typedef enum { SPI_1 = (int)SPI1_BASE, SPI_2 = (int)SPI2_BASE, SPI_3 = (int)SPI3_BASE, SPI_4 = (int)SPI4_BASE } SPIName; typedef enum { I2C_1 = (int)I2C1_BASE, I2C_2 = (int)I2C2_BASE, I2C_3 = (int)I2C3_BASE, FMPI2C_1 = (int)FMPI2C1_BASE } I2CName; typedef enum { PWM_1 = (int)TIM1_BASE, PWM_2 = (int)TIM2_BASE, PWM_3 = (int)TIM3_BASE, PWM_4 = (int)TIM4_BASE, PWM_5 = (int)TIM5_BASE, PWM_8 = (int)TIM8_BASE, PWM_9 = (int)TIM9_BASE, PWM_10 = (int)TIM10_BASE, PWM_11 = (int)TIM11_BASE, PWM_12 = (int)TIM12_BASE, PWM_13 = (int)TIM13_BASE, PWM_14 = (int)TIM14_BASE } PWMName; typedef enum { CAN_1 = (int)CAN1_BASE, CAN_2 = (int)CAN2_BASE } CANName; #ifdef __cplusplus } #endif #endif 将英文注释翻译成中文注释
最新发布
06-10
### C语言中 `typedef enum` `enum` 的用法及区别 #### 使用 `enum` 枚举类型 (`enum`) 是一种用户自定义的数据类型,在这种情况下,变量可以被赋予一组预定义的常量名称之一。这使得代码更具有可读性维护性。 ```c enum color { red = 0, blue, /* 默认为1 */ yellow /* 默认为2 */ }; ``` 通过上述方式定义了一个名为 `color` 的枚举类型,并设置了三个成员:`red`, `blue`, `yellow`[^1]。可以直接创建该类型的实例: ```c enum color myColor; myColor = blue; ``` 然而,每次使用此枚举类型都需要写上完整的前缀 `enum color`,这对于频繁使用的类型来说显得冗长且不便。 #### 使用 `typedef enum` 为了简化枚举类型的声明过程并提高代码简洁度,可以通过 `typedef` 关键字来给新定义的枚举类型起一个别名。这样以后就可以直接利用这个新的名字作为类型标识符了。 ```c typedef enum { red = 0, blue, /* 自动设置为1 */ yellow /* 继续自动增加到2 */ } ColorAlias; ``` 此时已经不需要每次都带上 `enum` 前缀就能方便地声明相应类型的对象了: ```c ColorAlias anotherColor; anotherColor = yellow; ``` 这种方式不仅减少了重复输入的工作量,还提高了程序的一致性易读性[^3]。 #### 总结两者的差异 当仅使用 `enum` 定义时,虽然能够成功建立一个新的枚举类别,但在后续应用过程中仍需保留原始形式;而借助 `typedef` 则允许开发者为其指定更加直观的名字以便日后调用。因此,在实际开发当中推荐采用后者以增强源码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山一角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值