学过c语言的一定会的问题,来看看,

来源:百度知道 编辑:UC知道 时间:2024/05/05 17:46:55
What is the output of the following program?
#include <stdio.h>
#define square (x) (x*)
main(void) {
int a=10;
printf("square (%d) = %d\n",a-2,square(a-2));
return 0;
}

square(8) = 64
square(8) = 16
square(8) = -12
None of the above

没有学过c语言,但学过Java不知道是不是第一个,如果是感觉太简单了,如果不是的,请您告诉我为什么

#define square (x) (x*)应该是#define square (x) (x*x)吧,不然这个程序应该编译不通过。

结果应该是square(8) = -12。

printf("square (%d) = %d\n",a-2,square(a-2)); 这一行经文本替换后是:
printf("square (%d) = %d\n",a-2,a-2*a-2);
引号里面的不替换,引号外面的只作简单文本替换,不自动加括号。