#宏的用法
#define VERSION "6.0.1"
#define DATETIME "datetime"
//将变量 s 以字符串的形式输出
#define xstr(s) __str(s)
#define __str(s) #s
//使用宏拼接字符串
#define ECHO_STR ("jemalloc-" xstr(hello) "." xstr(world) "." xstr(version))
//printf ("%s\n", ECHO_STR); 将输出 jemalloc-hello.world.version
//同时,还可以使用如下方式拼接字符串
char* buf = VERSION DATETIME;
printf ("%s\n", buf); //将输出buf的值为 6.0.1datetime
上述代码中,为什么需要使用两个嵌套宏函数来完成字符串是输出呢,看如下例子就能知道结果:
printf ("%s\n", xstr (VERSION)); //输出 "6.0.1"
printf ("%s\n", _str(VERSION)); //输出 VERSION
后者不会对参数进行宏替换,直接当做字符串输出,前者才对参数进行宏替换
#零长数组和变长数组
##零长数组
在 GNU c 中允许零长数组,在 c++ 和 ANSI C 中都不允许使用,如下所示:
typedef struct sds {
unsigned int len;
unsigned int free;
char buf[0];
};
此结构体中包含一个长度为零的数组,但是它的使用必须满足一定的