c - 预处理器运算符 "defined"的优先级?

标签 c operators c-preprocessor

我正在研究一个 c 预处理器并且发现,由于“defined”是一个预处理器专用运算符,它的优先级从未在其他 c 运算符中列出。因为它是一元的和合乎逻辑的,所以我会把它放在第二层,但是......

有谁知道确切答案吗?

最佳答案

C 2018 6.10.1 1 说:

The expression that controls conditional inclusion shall be an integer constant expression except that… it may contain unary operator expressions of the form “defined identifier” or “defined ( identifier )”… [Note: The text in quotes here is offset display text in the original.]

短语“一元运算符表达式”指的是 6.5.3(“一元运算符”),6.5(“表达式”)的一个小节。因此,defined 的行为类似于任何其他一元运算符。

但是请注意,操作数必须是标识符。它不能是大多数普通运算符接受的一般unary-expressioncast-expression。正是那些 unary-expressioncast-expression 符号将更高优先级的运算符引入到一元表达式的语法中。这意味着像 #if defined x++ 这样的东西是不允许的(甚至在考虑 ++ 是否可能出现在整数常量表达式中之前),所以没有任何其他选择. “defined identifier”永远不会与标识符相邻的任何更高优先级运算符一起出现。

关于c - 预处理器运算符 "defined"的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53346559/

相关文章:

c - C语言"Expected expression"错误

python - 如何在 python 中定义自己的前缀和后缀运算符

c - 为什么 C 中运算符之间的关联性不同?

c - 如何基于可变宏扩展多个宏

c++ - 如何可视化 C/C++ #define 指令?

c++ - 使用 g++ 和 specs 文件进行预处理

使用 ARM 进行 C 编程 - 将结构输出和输入到文件

对 getchar() 和 fflush(stdin) 行为感到困惑

c - 更好地理解 c 中可变参数的类型提升

r - ggplot2 plus 运算符是如何定义的?