本节主要分析宏定义的特殊用法。
一、#与##
1.#
eg:x是一个宏参数,那么#x可以把参数名转换成相应的字符串,这个过程叫字符串化。
test1.c
[cpp] view plain copy
#include <stdio.h>
#define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x))
int main(void)
{
int y =4
PSQR(y)
PSQR(2+4)
return 0
}
/*result:
the square ofyis 16.
the square of2+4is 36.
*/
gcc test1.c -o test1
./test1
[plain] view plain copy
the square ofyis 16.
the square of2+4is 36.
2.##
该运算符把两个语言符号组合成单个语言符号。
test2.c
[cpp] view plain copy
#include <stdio.h>
#define XNAME(n) x##n
#define PXN(n) printf("x"#n" = %d\n",x##n)
int main(void)
{
int XNAME(1)=12//int x1=12
PXN(1)//printf("x1 = %d\n", x1)
return 0
}
/*result:
x1 = 12
*/
gcc test2.c -o test2
./test2
[plain] view plain copy
x1 = 12
二、可变参数宏与__VA_ARGS__
__VA_ARGS__是一个可变参数宏,是新的C99规范中新增的,目前只有gcc支持(VC6.0的编译器不支持)。
宏定义中参数列表的最后一个参数为省略号(也就是三个点)。这样预定义宏_ _VA_ARGS_ _就可以被用在替换部分中,替换省略号所代表的字符串。
简单例子:
test3.c
[cpp] view plain copy
#include <stdio.h>
#define PR(...) printf(__VA_ARGS__)
int main()
{
int wt=1,sp=2
PR("hello\n")
PR("weight = %d, shipping = %d\n",wt,sp)
return 0
}
/*result:
hello
weight = 1, shipping = 2
*/
gcc test3.c -o test3
./test3
[plain] view plain copy
hello
weight = 1, shipping = 2
三、在Linux内核系统调用中的应用
公共接口:
common.h(相当与内核中的systemcalls.h)
[cpp] view plain copy
#define __SC_DECL1(t1, a1)t1 a1
#define __SC_DECL2(t2, a2, ...) t2 a2, __SC_DECL1(__VA_ARGS__)
#define __SC_DECL3(t3, a3, ...) t3 a3, __SC_DECL2(__VA_ARGS__)
#define __SC_DECL4(t4, a4, ...) t4 a4, __SC_DECL3(__VA_ARGS__)
#define __SC_DECL5(t5, a5, ...) t5 a5, __SC_DECL4(__VA_ARGS__)
#define __SYSCALL_DEFINEx(x, name, ...) \
long sys##name(__SC_DECL##x(__VA_ARGS__))
#define SYSCALL_DEFINEx(x, sname, ...) \
__SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
#define SYSCALL_DEFINE0(name) long sys_##name(void)
#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)
#define SYSCALL_DEFINE5(name, ...) SYSCALL_DEFINEx(5, _##name, __VA_ARGS__)
1.无参数
test4.c
[cpp] view plain copy
#include <stdio.h>
#include "common.h"
long sys_tank(void)
long sys_tankai(void)
SYSCALL_DEFINE0(tank)
{
printf("TK--------_>>>>>>tank call ok!!!\n")
}
SYSCALL_DEFINE0(tankai)
{
printf("TK--------_>>>>>>tankai call ok!!!\n")
}
int main(){
sys_tank()
sys_tankai()
return 0
}
/*result:
TK----->>>lianjie(a,b) is 20
TK----->>>lianjie(4,5) is 45
TK--------_>>>>>>tank call ok!!!
TK--------_>>>>>>tankai call ok!!
*/
gcc test4.c -o test4
./test4
[plain] view plain copy
TK----->>>lianjie(a,b) is 20
TK----->>>lianjie(4,5) is 45
TK--------_>>>>>>tank call ok!!!
TK--------_>>>>>>tankai call ok!!
2.一个参数
test5.c
[cpp] view plain copy
#include <stdio.h>
#include "common.h"
long sys_tank(int fd)
SYSCALL_DEFINE1(tank, int, fd)
{
printf("TK--------_>>>>>>call ok!!!>>fd is %d\n",fd)
return 0
}
int main(){
sys_tank(3)
return 0
}
/*
SYSCALL_DEFINE1(tank,int,fd) = SYSCALL_DEFINEx(1, _tank, int, fd)
SYSCALL_DEFINEx(1, _tank, int, fd) = __SYSCALL_DEFINEx(1, _tank, int, fd)
__SYSCALL_DEFINEx(1, _tank, int, fd) = long sys_tank(__SC_DECL1(int,fd))
long sys_tank(__SC_DECL1(int,fd))
//__SC_DECL1(int,fd) = int fd
//#define __SC_DECL1(t1, a1)t1 a1
//#define __SC_DECL2(t2, a2, ...) t2 a2, __SC_DECL1(__VA_ARGS__)
//#define __SC_DECL3(t3, a3, ...) t3 a3, __SC_DECL2(__VA_ARGS__)
long sys_tank(__SC_DECL1(int,fd)) = long sys_tank(int fd)
*/
/*result:
TK--------_>>>>>>call ok!!!>>fd is 3
*/
gcc test5.c -o test5
./test5
[plain] view plain copy
TK--------_>>>>>>call ok!!!>>fd is 3
3.五个参数情况,其他不再罗列
test6.c
[cpp] view plain copy
#include <stdio.h>
#include "common.h"
long sys_mount(char *dev_name, char *dir_name, char *type, unsigned long flags, void *data)
SYSCALL_DEFINE5(mount, char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data)
{
printf("TK--------_>>>>>>call ok!!!>>>dev_name is %s,dir_name is %s,flags is %d\n",dev_name,dir_name,flags)
return 0
}
int main(){
sys_mount(".", "/", NULL, 0, NULL)
return 0
}
/*
SYSCALL_DEFINE5(mount,char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data) = SYSCALL_DEFINEx(5, _mount, char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data)
SYSCALL_DEFINEx(5, _mount, char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data) = __SYSCALL_DEFINEx(5, _mount, char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data)
__SYSCALL_DEFINEx(5, _mount, char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data) = long sys_tank(__SC_DECL5(char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data))
long sys_tank(__SC_DECL1(int,fd))
//__SC_DECL5(char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data) = char * dev_name, __SC_DECL4(char *, dir_name, char *, type, unsigned long, flags, void *, data)
//......
//__SC_DECL5(char *, dev_name, char *, dir_name, char *, type, unsigned long, flags, void *, data) = char *dev_name, char *dir_name, char *type, unsigned long flags, void *data
//#define __SC_DECL1(t1, a1)t1 a1
//#define __SC_DECL2(t2, a2, ...) t2 a2, __SC_DECL1(__VA_ARGS__)
//#define __SC_DECL3(t3, a3, ...) t3 a3, __SC_DECL2(__VA_ARGS__)
//#define __SC_DECL4(t4, a4, ...) t4 a4, __SC_DECL3(__VA_ARGS__)
//#define __SC_DECL5(t5, a5, ...) t5 a5, __SC_DECL4(__VA_ARGS__)
long sys_mount(__SC_DECL5(int,fd)) = long sys_mount(char *dev_name, char *dir_name, char *type, unsigned long flags, void *data)
*/
/*result:
//TK--------_>>>>>>call ok!!!>>>dev_name is .,dir_name is /,flags is 0
*/
gcc test6.c -o test6
./test6
[plain] view plain copy
TK--------_>>>>>>call ok!!!>>>dev_name is .,dir_name is /,flags is 0
这里用宏的好处太多了。1、增强代码的可读性。
拿这个例子来说,其他程序员一看这个宏就知道,0x01表示大写字母类型,0x02表示小写字母类型,0x03表示数字类型。然后isalnum是字符c类型判断结果,为零表示c不是字母或数字,不为零则表示c是字母或数字。
2、减少代码运行消耗。
if(isalnum)和if(__ctype[c] &(UPPER|_LOWER|DIGIT))相比,不止是写起来简单、看起来清晰,它的运行效率也更高了吧。
3、增强代码的可移植性。
还是这个例子,如果要求在程序中只需要判断一个字符是否为大写字母,那么只需要把宏改为#define isUpper (__ctype[c] &_UPPER)就可以实现,而不用去代码中找到并修改每一个相关的判断语句。
在linux c编程中学会使用宏,会给你带来很多好处和。。乐趣,呵呵。希望我的回答能对你有些帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)