我們用一來實(shí)例來學(xué)習(xí)一下C++宏定義中的## 和#的區(qū)別,首先我們來定義一個(gè)宏:#define STRCPY(a, b) strcpy(a ## _p, #b)
1. 宏定義里面有個(gè)##表示把字符串聯(lián)在一起。如:
#include
#define CAT(x,y) x##y
int main()
{
printf("%s", CAT("hello", " world"));
return 0;
}
2.宏定義中的#表示將其變?yōu)樽址H纾?BR> #include
#include
#define STRCPY(a, b) strcpy(a ##_p, #b)
int main()
{
char arrr_p[]="abcdefg";
char *b = "123456";
STRCPY(arrr, b);
return 0;
}
結(jié)果為把b變成了字符串,可見#b的功能是將所有類型名都變成了字符串。 另,a、_p和##有沒有空格不影響結(jié)果。
1. 宏定義里面有個(gè)##表示把字符串聯(lián)在一起。如:
#include
#define CAT(x,y) x##y
int main()
{
printf("%s", CAT("hello", " world"));
return 0;
}
2.宏定義中的#表示將其變?yōu)樽址H纾?BR> #include
#include
#define STRCPY(a, b) strcpy(a ##_p, #b)
int main()
{
char arrr_p[]="abcdefg";
char *b = "123456";
STRCPY(arrr, b);
return 0;
}
結(jié)果為把b變成了字符串,可見#b的功能是將所有類型名都變成了字符串。 另,a、_p和##有沒有空格不影響結(jié)果。

