【问题标题】:Value of a Pre-processor directive statement in CC 中预处理器指令语句的值
【发布时间】:2020-02-21 09:03:18
【问题描述】:

所以,我很困惑为什么 i 的输出是 27 而不是 343

 #include <stdio.h>
#define x 5+2
void main() {
int i;
i=x*x*x;
printf("%d",i); }

【问题讨论】:

标签: c


【解决方案1】:

让我们扩展你所做的,预处理器是一个虚拟的东西,只是用文本替换标记

i = 5+2*5+2*5+2; // <- 27

如果您将define 修改为#define x (5+2),则一切都应按预期工作。

【讨论】:

  • 这也是一般的建议:总是用括号括起来一个宏表达式。
  • 仅供参考,宏替换替换预处理器标记,而不是文本。一个区别是宏替换不能形成新的预处理器标记(除非使用### 运算符)。
  • @EricPostpischil 不错,将用token 替换text
猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多