ºìÁªLinuxÃÅ»§
Linux°ïÖú

¼òµ¥½éÉÜ Linux ÖÐµÄ C ÓïÑÔÃîÓÃ

·¢²¼Ê±¼ä:2006-08-01 08:37:11À´Ô´:ºìÁª×÷Õß:artiomgy
1.Á½¸öÊý±È½Ï´óС

#define min( x,y) ({ \

typeof( x) _x = ( x); \

typeof( y) _y = ( y); \

(void) (&_x == &_y); \ //ÃîÓã¡×÷ÓÃÊǼì²é²ÎÊý x ºÍ y µÄÀàÐÍÊÇ·ñÏàͬ

_x < _y ? _x : _y; })


2.Á´±íµÄʹÓÃ

Õâ¸ö²»¶à˵ÁË£¬Ì«°ôÁË£¬¿ÉÒÔÓÉÒ»¸öÁ´±í³ÉÔ±»ñÈ¡ËÞÖ÷µÄÖ¸Õë

3.do{}while(0)µÄʹÓÃ

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

4.½á¹¹ÌåÖеÄ#define

struct sock {

struct sock_common __sk_common;

#define sk_family __sk_common.skc_family

#define sk_state __sk_common.skc_state

.....

};

5.ʹÓÃCд³öµÄÃæÏò¶ÔÏó³ÌÐò£¬Í¨¹ý¸ø½á¹¹ÌåÖеijÉÔ±¸³Ó費ͬµÄº¯ÊýÖ¸Õë¶øʵÏÖ²»Í¬µÄ¹¦ÄÜ£¬È¡´úÁË´óÁ¿µÄcaseÓï¾ä

6.ͨ¹ýÌõ¼þºê¶¨Ò壬ͬһº¯ÊýÃûÓв»Í¬µÄʵÏÖ£¬·½±ãµ÷Óô˺¯Êý

#ifdef xxx

#define func(x) do{ʵÏÖ£»}while(0)

#else

#define func(x) do{}while(0)

7.Á㳤¶ÈÊý×é

GNU C ÔÊÐíʹÓÃÁ㳤¶ÈÊý×飬ÔÚ¶¨Òå±ä³¤¶ÔÏóµÄÍ·½á¹¹Ê±£¬Õâ¸öÌØÐԷdz£ÓÐÓá£Àý

È磺

struct minix_dir_entry {

__u16 inode;

char name[0];

};

8.¿É±ä²ÎÊýºê

ÔÚ GNU C ÖУ¬ºê¿ÉÒÔ½ÓÊܿɱäÊýÄ¿µÄ²ÎÊý£¬¾ÍÏóº¯ÊýÒ»Ñù£¬ÀýÈ磺

#define pr_debug(fmt,arg...) \

printk(KERN_DEBUG fmt,##arg)
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. iLove.Rain ÓÚ 2006-08-10 15:56:46·¢±í:

    ²»´í