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

标签 c macros microcontroller avr

您好,我在使用 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/

相关文章:

C错误:storage size of ffblk isn't known

c++ - Coverity 扫描设置?

c - c中数组指针的解释?

macros - 使用宏生成函数(或其他宏)时避免符号捕获

c - mikroc for pic 中的任务出错

C: typedef union

C strcmp 不工作

c - 如何使用宏中定义的字符串/将其转换为整数?

scala - 避免在函数内装箱/拆箱

timer - 在 AT89S52、4Mhz 晶振下以 9600 波特发送数据