1 #include <stdio.h> 2 #define SQR(X) X*X 3 4 int main(int argc, char* argv[]) 5 { 6 int a = 10; 7 int k = 2; 8 int m = 1; 9 10 printf("SQR(k+m) = %d\n", SQR(k+m)); //SQR(k+m) = 5 11 printf("SQR(k+m)/SQR(k+m) = %d\n", SQR(k+m)/SQR(k+m)); //SQR(k+m)/SQR(k+m) = 7 12 printf("SQR(k+m)/SQR(k+m) = %.2f\n", 1.0*SQR(k+m)/SQR(k+m)); //SQR(k+m)/SQR(k+m) = 7.00 13 printf("SQR(k+m)/SQR(k+m) = %.2f\n", SQR(k+1.0*m)/SQR(k+m)); //SQR(k+m)/SQR(k+m) = 7.50 14 printf("a/(SQR(k+m)/SQR(k+m)) = %d\n", a/(SQR(k+m)/SQR(k+m))); //a/(SQR(k+m)/SQR(k+m)) = 1 15 printf("1.0*a/(SQR(k+m)/SQR(k+m)) = %.2f\n",1.0*a/(SQR(k+m)/SQR(k+m)));//1.0*a/(SQR(k+m)/SQR(k+m)) = 1.43 16 17 return 0; 18 } 19 20 /* 21 SQR(k+m) = 5 22 SQR(k+m)/SQR(k+m) = 7 23 SQR(k+m)/SQR(k+m) = 7.00 24 SQR(k+m)/SQR(k+m) = 7.50 25 a/(SQR(k+m)/SQR(k+m)) = 1 26 1.0*a/(SQR(k+m)/SQR(k+m)) = 1.43 27 28 以上测试得: 29 SQR(k+m)/SQR(k+m)表达式展开替换为: 1.0*SQR(k+m)/SQR(k+m)表达式展开替换为: 30 k+m*k+m/k+m*k+m = 2 + 1*2 +1/2 +1*2 + 1 1.0*k+m*k+m/k+m*k+m = 1.0*2 + 1*2 +1/2 +1*2 + 1 31 = 2 + 1 + 1/2 + 3 + 1 = 2.0 + 1 + 1/2 + 3 + 1 32 = 3 + 1/2 + 4 = 2.0 + 1 + 0 + 3 + 1 33 = 3 + 0 + 4 = 2.0 + 5 34 =7 = 7.0 35 1.0*(SQR(k+m)/SQR(k+m))表达式展开替换为: 36 = 1.0*(k+m*k+m/k+m*k+m ) 37 = 1.0 * 7 38 = 7.0 39 SQR(k+1.0*m)/SQR(k+m) = 40 = k+1.0*m*k+1.0*m/k+m*k+m 41 = 2 + 1.0*2 + 1.0/2 + 1*2 + 1 42 = 2 + 2.0 + 0.5 + 2 + 1 43 = 2.0 + 2.0 + 0.5 + 2.0 + 1.0 44 = 7.50 45 总结: 46 define定义的宏变量(以及相应的头文件等),在编译前进行代码替换,注意仅仅是代码替换,并不涉及到运算符等的操作,因为运算符操 47 作是在编译阶段进行的 48 define 只是定义而已,在编择时只是进行简单代换而已,并不经过任何其他的处理(例如:加减等运算或者是附加括号等的结合性干预) 49 表达式中的运算是按“块”来分步运算的,各个块中按照块中各成员精度最高者运算并按照最高精度得出结果。 50 51 */
扩展阅读:C/C++源代码到可执行程序的过程详解