参考自keil的RTX源码:
/// Thread Definition structure contains startup information of a thread.
typedef struct os_thread_def {os_pthread pthread; ///< start address of thread functionosPriority tpriority; ///< initial thread priorityuint32_t instances; ///< maximum number of instances of that thread functionuint32_t stacksize; ///< stack size requirements in bytes; 0 is default stack size
} osThreadDef_t; // 定义结构体类型// 宏定义
#define osThreadDef(name, priority, instances, stacksz)
const osThreadDef_t os_thread_def_##name =
{ (name), (priority), (instances), (stacksz) }osThreadDef(AppSysRunThread,osPriorityNormal,1,0);
// 定义一个结构体变量,同时进行初始化//宏定义osThreadDef展开的结果如下:
const osThreadDef_t os_thread_def_AppSysRunThread =
{(AppSysRunThread), (osPriorityNormal), (1), (0)};//即:
const osThreadDef_t os_thread_def_AppSysRunThread =
{ (AppSysRunThread), (osPriorityNormal), (1), (0),
};
“##”被称为连接符(concatenator),用来将两个Token连接为一个Token。注意这里连接的对象是Token就行,而不一定 是宏的变量。“##”前后可以有空格。
// 宏定义
#define A1(name, type) type name_##type##_type
#define A2(name, type) type name##_##type##_type// 宏定义展开
A1(a1, int);
// 等价于:
int name_int_type;A2(a2, int);
// 等价于:
int a2_int_type;
需要注意的是凡宏定义里有用’#‘或’##'的地方宏参数是不会再展开的。
#define A (2)
define CONS(a,b) int(a##e##b) printf("%sn", CONS(A, A)); // 这一行被展开为:
printf("%sn", int(AeA)); // A都不会再被展开
参考来源:.html
参考来源:
自己想当然的尝试:
#include<stdio.h>
#include<string.h>#define TRANS(x) #x/*# ## 只能用在宏定义中*/unsigned char str[]=
{"hello" " "TRANS(world) // 不能将 TRANS(world) 改为 #world 没有这样的用法"n"
};int main(void)
{printf("%s", str);printf("%ldn", sizeof(str));return 0;
}/*
运行结果如下:ubuntu@ubuntu:~$ ./a.out
hello world
13
ubuntu@ubuntu:~$数组中的元素分别是:
'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' 'n' '