gpt4 book ai didi

c - C 中用于替换另一个宏中的参数的宏

转载 作者:行者123 更新时间:2023-11-30 18:13:53 26 4
gpt4 key购买 nike

您好,我在使用 C 宏时遇到问题,想知道是否有人可以提供帮助:

#define A7 PORTA,7
#define SET_BIT(x,y) x=x|(1<<y)

我正在尝试设置宏,以便在主代码中,我只需键入

SET_BIT(A7); to set the 7th bit of A7

但是,我收到以下错误:

warning C4003: not enough actual parameters for macro 'SET_BIT'
error C2106: '=' : left operand must be l-value
error C2059: syntax error : ')'

如有任何帮助,我们将不胜感激。

最佳答案

您还可以通过使用中间宏来按照您想要的方式进行操作,该中间宏会将参数扩展为用逗号分隔的两个参数。

#define A7 PORTA,7
#define SET_BIT_INNER(x,y) x=(x)|(1<<(y))
#define SET_BIT(x) SET_BIT_INNER(x)

SET_BIT(A7);

正如 valter 所说,不要忘记宏中使用的每个参数周围的括号。

关于c - C 中用于替换另一个宏中的参数的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21317269/

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