| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 软件开发

简单介绍 Linux 中的 C 语言妙用

时间:2006-08-02 10:10:21  来源:Linux宝库  作者:

1.两个数比较大小 AwCLinux联盟

#define min( x,y) ({ \ AwCLinux联盟

typeof( x) _x = ( x); \ AwCLinux联盟

typeof( y) _y = ( y); \ AwCLinux联盟

(void) (&_x == &_y); \ //妙用!作用是检查参数 x 和 y 的类型是否相同 AwCLinux联盟

_x < _y ? _x : _y; }) AwCLinux联盟

2.链表的使用 AwCLinux联盟

这个不多说了,太棒了,可以由一个链表成员获取宿主的指针 AwCLinux联盟

3.do{}while(0)的使用 AwCLinux联盟

#define func(x) do{...;}while(0) AwCLinux联盟

4.结构体中的#define AwCLinux联盟

struct sock { AwCLinux联盟

struct sock_common __sk_common; AwCLinux联盟

#define sk_family __sk_common.skc_family AwCLinux联盟

#define sk_state __sk_common.skc_state AwCLinux联盟

..... AwCLinux联盟

}; AwCLinux联盟

5.使用C写出的面向对象程序,通过给结构体中的成员赋予不同的函数指针而实现不同的功能,取代了大量的case语句 AwCLinux联盟

6.通过条件宏定义,同一函数名有不同的实现,方便调用此函数 AwCLinux联盟

#ifdef xxx AwCLinux联盟

#define func(x) do{实现;}while(0) AwCLinux联盟

#else AwCLinux联盟

#define func(x) do{}while(0) AwCLinux联盟

7.零长度数组 AwCLinux联盟

GNU C 允许使用零长度数组,在定义变长对象的头结构时,这个特性非常有用。例 AwCLinux联盟

如: AwCLinux联盟

struct minix_dir_entry { AwCLinux联盟

__u16 inode; AwCLinux联盟

char name[0]; AwCLinux联盟

}; AwCLinux联盟

8.可变参数宏 AwCLinux联盟

在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如: AwCLinux联盟

#define pr_debug(fmt,arg...) \ AwCLinux联盟

printk(KERN_DEBUG fmt,##arg) AwCLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
    无相关信息
栏目更新
栏目热门