C語言巨集定義中的 , , 及 符號的作用

2021-06-09 04:01:42 字數 1700 閱讀 6317

c語言巨集定義中的#,##,#@及\符號的作用

1、# (stringizing字串化操作符

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

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

#define example1(instr) #instr

當使用該巨集定義時:

example(abc); 在編譯時將會展開成:printf("theinput string is:\t%s\n","abc");

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

*注意:

對空格的處理:

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

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

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

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

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

作用:將巨集定義的多個形參轉換成乙個實際引數名。 如:

#defineexamplenum(n) num##n

intnum9=9;

使用:

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

注意:

a、當用##連線形參時,##前後的空格可有可無。

如:#defineexamplenum(n) num  ##  n 相當於 #defineexamplenum(n) num##n

b、連線後的實際引數名,必須為實際存在的引數名或是編譯器已知的巨集定義。

另:如果##後的引數本身也是乙個巨集的話,##會阻止這個巨集的展開

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

int main()

3、#@

(charizing)字元化操作符

作用:將傳入單字元引數名轉換成字元,以一對單引用括起來。 如:

#definemakechar(x) #@x

a = makechar(b);

展開後變成了:

a= 'b';

4、\續行操作符

當定義的巨集不能用一行表達完整時,可以用""表示下一行繼續此巨集的定義。注意\前最好留空格

巨集定義中允許包含兩行以上命令的情形,此時必須在最右邊加上"\"且該行"\"後不能再有任何字元,連注釋部分都不能有。下面的每行最後的一定要是"\","\"後面加乙個空格都會報錯,更不能跟注釋。

#define exchange(a,b)

定義多行巨集:注意斜槓的使用,最後一行不能用斜槓.

C語言巨集定義中的 , , 及 符號的作用

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

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

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

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

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