C#定义奇怪的输出

标签 c output

#include <stdio.h>
#define test(x) x*x
#define test2(x) x
int main(void) {    
int x=5;
printf("%d %d %d %d %d %d",test2(x), test2(x-2), test(x), test(x-2),4*(test(x-3)),4*test(x-3));
return 0;
}

输出如下:

5 3 25 -7 -52 2

很好理解前 3 个,但为什么 test(x-2) 时为 -7,最后 2 个……

最佳答案

编译器预处理步骤之后,您的 printf 变为

printf("%d %d %d %d %d %d",x, x-2, x*x, x-2*x-2,4*(x-3*x-3),4*x-3*x-3);

x-2*x-2 计算为: x-2*x-2 -> x-(2*x)-2

5-10-2

=-7

ie * 优先于 -

关于C#定义奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43906246/

相关文章:

java - 尝试使用 input.nextLine() 用用户输入(字符串)填充数组,但打印输出两次并且第一次不要求输入

c - 当写入 txt 的输入数据大于 16kb 时应用程序崩溃(C 编程)

c - 使用二维数组的格雷码 (C)

c - 指向自身的指针数组

c - set_intersection 适用于整数,但不适用于字符,即使它是 void*

c++ - 当我以 "wb"模式写入文件时,会发生什么?

java - 填写空白字符串

c++ - 在 main 中调用构造函数,但未正确创建对象

c - 段错误:11

string - 如何在 ColdFusion 中输出八进制值