易出錯的C語言題目之一 巨集定義與預處理

2022-07-04 13:15:08 字數 533 閱讀 7712

#include#include

#define strcpy(a,b) strcpy(a##_p,#b)

intmain()

答案:var1 = var2/nvar2 = var1/n

解析:#為字串化操作符,將所連線的部分轉化為乙個字串,##為字串連線符,將兩個字串連線成乙個字串,均在預處理時完成字串的替換。

#include#define f(a,b) a##b

#define g(a) #a

#define h(a) g(a)

intmain()

答案:12

f(1,2)

解析:本題考察字串化操作符#得作用得執行過程,以及巨集定義替換的次序。如果該巨集為帶#號的巨集定義,則直接替換字串,無需再巢狀替換,如果該巨集不是帶#的巨集,則先替換括號內部的巨集定義,然後替換外部的。

答案:#define offset(struct, member)  ((unsigned int)&((struct *)0)->member)

C語言巨集定義中的 與

1 為後面的巨集形參新增雙引號。例 define singlesharp arg arg 注意 和arg之間可以有空格,效果一樣 singlesharp singlesharp singlesharp 2 為後面的巨集形參新增單引號。例 define singlesharpwithat arg ar...

易錯的C語言題目

這道題是我在網上看到的。拿出來給大家一起學習學習 include int main void int p int a 1 printf p 1 d,p 1 d,p d n p 1 p 1 p printf d d sizeof a sizeof a return 0 題目看似簡單,其實重點難點在於理...

c語言的巨集定義

include define square x x x define pr x printf the result is d.n x int main void 執行結果如下 the result is 16.the result is 4.the result is 14.the result i...