c中括号的使用

关于括号的使用,以下 2 个陈述有什么区别?(没有指针左右)

#define UART1_BAUD (460800)
#define UART2_BAUD 9600

回答

当宏通常用作表达式中的操作数时,没有区别。

但是请注意,在

#define A 4 + 7
#define B (5 + 3)

如果您将宏用作

int a = 6 * A; // 6 * 4 + 7 ==> 24 + 7
int b = 6 * B; // 6 * (5 + 3) ==> 6 * 8

根据经验:在宏中使用和滥用括号

当括号紧跟在宏名后面时,它是一个类似函数的宏

#define SQUARE(BAR) ((BAR) * (BAR)) // use and abuse parenthesis

  • “No difference whatsoever” is incorrect. `UART2_BAUD` could be used in situations with the concatenation operator `##` to form an identifier. `UART1_BAUD` cannot.

以上是c中括号的使用的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>