C語言巨集替換的幾種用法 【轉載】
阿新 • • 發佈:2019-01-21
①簡單巨集替換
#defind Pi 3.14159
或
#ifndef __THIS_FILE__
#define __THIS_FILE__ // 用於防止重複包含檔案
…… ……
#endif
___________________________
②條件巨集替換
#define p(x) printf(x)
…… ……
p("hello!");
→ hello!
___________________________
③字元巨集替換(#@)
#define pchar(x) printf("%c\n", #@x)
…… ……
pchar(a);
→ a
___________________________
④字串巨集替換(#)
#define pstring(x) printf("%s\n", #x)
…… ……
pstring(hello!);
→ hello!
___________________________
⑤連線巨集替換(##)
#define p( n ) printf( "symbol" #n " = %d", symbol##n )
…… ……
int symbol9 = 9;
p( 9 );
→ symbol9=9
#defind Pi 3.14159
或
#ifndef __THIS_FILE__
#define __THIS_FILE__ // 用於防止重複包含檔案
…… ……
#endif
___________________________
②條件巨集替換
#define p(x) printf(x)
…… ……
p("hello!");
→ hello!
___________________________
③字元巨集替換(#@)
#define pchar(x) printf("%c\n", #@x)
…… ……
pchar(a);
→ a
___________________________
④字串巨集替換(#)
#define pstring(x) printf("%s\n", #x)
…… ……
pstring(hello!);
→ hello!
___________________________
⑤連線巨集替換(##)
#define p( n ) printf( "symbol" #n " = %d", symbol##n )
…… ……
int symbol9 = 9;
p( 9 );
→ symbol9=9