gpt4 book ai didi

c - C中的预编译消息

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:55 24 4
gpt4 key购买 nike

我正在使用 C 语言编写一个在 ATMEGA32 中配置外部中断的函数

    typedef enum{
LOW_LVL,
CHANGE,
FALLING,
RISING
}TRIGGER;

void configExtInt(uint8_t ExtIntNo, TRIGGER trig){
sei();
if (ExtIntNo == INT0){

}else if (ExtIntNo == INT1){

}else if (ExtIntNo == INT2){

}else{
cli();
/* warning message here*/
}
}

如果我的函数的用户向我的函数提供了 INT0、INT1、INT2 以外的参数,我想在编译期间显示一条警告消息。

例如:

configExtInt (INT3, FALLING);

这可能吗?

最佳答案

[不确定我是否真的理解正确你的问题]

假设 INTx 是枚举,您可以使用一个开关来代替那几个 if-thens。

一个体面的 GCC 然后会警告你丢失的案例:

enum Ints
{
INT0,
INT1,
INT2
}

int main(void)
{
enum Ints e = ...; /* Initialise to some value here. */

switch (e)
{
case INT0:
break;

case INT1:
break;
}

return;
}

编译这个使用

gcc -g  -Wall  -Wextra -Wconversion -pedantic  main.c

并得到:

main.c:12:3: warning: enumeration value ‘INT2’ not handled in switch [-Wswitch]
switch (e)
^~~~~~

关于c - C中的预编译消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48488004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com