2008年9月25日 星期四

取得Macro的參數字串

除了一般function的寫法外,function也可以用macro來定義,例如定義一個可以印出某數的平方值:

#define SQUARE(x) printf("The square of x is %d\n", ((x)*(x)))
當程式碼始用
SQUARE(5);
則會輸出
The square of x is 25

我們可以利用#來取得macro參數的名字
#define SQUARE(x) printf("The square of " #x " is %d\n", ((x)*(x)))
輸出結果則會變成
The square of 5 is 25

0 意見: