/*C語言 巨集的錯誤範例*/
#include<stdio.h>
#include<stdlib.h>
#define square(n) n*n
/*定義巨集square(n)為n*n*/
int n = 5;
/*宣告全域變數n*/
int main(void)
{
int i = 10;
printf("n=%d,i=%d\n", n, i);
printf("square(n)=%d,square(i)=%d\n",
square(n),square(i));
printf("square(n+1)=%d,square(i+1)=%d\n",
square(n+1), square(i+1));
/*上面的錯誤巨集會使巨集的敘述為:
square(n+1*n+1)
square(i+1*i+1)
先乘除後加減,倒置答案錯誤*/
printf("(n+1)*(n+1)=%d,(i+1)*(i+1)=%d\n",
(n + 1) * (n + 1), (i + 1) * (i + 1));
system("pause");
return 0;
}.
結果為:
文章標籤
全站熱搜
留言列表