define巨集定義中的 , , 及 符號

2021-05-23 15:19:02 字數 1447 閱讀 4416

1、# (stringizing)字串化操作符。

其作用是:將巨集定義中的傳入引數名轉換成用一對雙引號括起來引數名字串。其只能用於有傳入引數的巨集定義中,且必須置於巨集定義體中的引數名前。

如: #define example(instr) printf("the input string is:/t%s/n",#instr) #define example1(instr) #instr

當使用該巨集定義時: example(abc); 在編譯時將會展開成:printf("the input string is:/t%s/n","abc");

string str=example1(abc); 將會展成:string str="abc";

注意: 對空格的處理

a。忽略傳入引數名前面和後面的空格。

如:str=example1( abc ); 將會被擴充套件成 str="abc";

b.當傳入引數名間存在空格時,編譯器將會自動連線各個子字串,用每個子字串中只以乙個空格連線,忽略其中多餘乙個的空格。

如:str=exapme( abc def); 將會被擴充套件成 str="abc def";

2、## (token-pasting)符號連線操作符

巨集定義中:引數名,即為形參,如#define sum(a,b) (a+b);中a和b均為某一引數的代表符號,即形式引數。

而##的作用則是將巨集定義的多個形參成乙個實際引數名。

如: #define examplenum(n) num##n int num9=9;

使用: int num=examplenum(9); 將會擴充套件成 int num=num9;

注意:

1.當用##連線形參時,##前後的空格可有可無。 如:#define examplenum(n) num ## n 相當於 #define examplenum(n) num##n

2.連線後的實際引數名,必須為實際存在的引數名或是編譯器已知的巨集定義 // preprocessor_token_pasting.cpp #include

#define paster( n ) printf_s( "token" #n " = %d", token##n )

int token9 = 9;

int main()

執行結果: token9 = 9

3、@# (charizing)字元化操作符。

只能用於有傳入引數的巨集定義中,且必須置於巨集定義體中的引數名前。作用是將傳的單字元引數名轉換成字元,以一對單引用括起來。 #define makechar(x) #@x a = makechar(b); 展開後變成了: a= 'b';

4、/ 行繼續操作符 當定義的巨集不能用一行表達完整時,可以用"/"表示下一行繼續此巨集的定義。

define巨集定義中的 , , 及 符號

1 stringizing 字串化操作符 其作用是 將巨集定義中的傳入引數名轉換成用一對雙引號括起來引數名字串。其只能用於有傳入引數的巨集定義中,且必須置於巨集定義體中的引數名前。如 define example instr printf the input string is t s n inst...

define巨集定義中的 , , 及 符號

include stdio.h define size x printf 11s t 2d n x,sizeof x int main int argc,char argv 1 stringizing 字串化操作符。其作用是 將巨集定義中的傳入引數名轉換成用一對雙引號括起來引數名字串。其只能用於有傳...

define巨集定義中的 , , 及 符號

1 stringizing 字串化操作符。其作用是 將巨集定義中的傳入引數名轉換成用一對雙引號括起來引數名字串。其只能用於有傳入引數的巨集定義中,且必須置於巨集定義體中的引數名前。如 define example instr printf the input string is t s n inst...