【发布时间】:2019-10-19 12:06:59
【问题描述】:
#include <stdio.h>
#define abs(x) (x < 0 ? -x : x)
int x;
int doubleGlobalX()
{
x*=2;
return x;
}
int main()
{
scanf("%d",&x);//5
printf("%d\n",abs(doubleGlobalX()));//20
return 0;
}
当输入为5 时,此代码的输出为20。当输入为15 时,输出为60。不知道为什么它把全局翻了两次。
【问题讨论】:
-
OT: about:
scanf("%d",&x);scanf()系列函数返回成功的“输入转换说明符”的数量(在这种情况下,除 1 之外的任何返回值都表示发生了错误。 -
OT: about:
#define abs(x) (x < 0 ? -x : x)始终在结果语句中的参数周围放置括号以避免“文本替换”错误
标签: c macros c-preprocessor