The Magical Use of C Language in Linux

The magic of C language in Linux
1. Compare the size of two numbers #define min( x,y) ({ \ typeof( x) _x = ( x); \ typeof( y) _y = ( y); \ (void) ( &_x == &_y); \ //Magic use! The function is to check whether the types of parameters x and y are the same _x < _y ? _x : _y; }) 2. The use of linked lists is not much to say, it is great, you can use A linked list member gets the pointer of the host 3. Use of 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. The object-oriented program written in C implements different functions by assigning different function pointers to the members of the structure, instead of A large number of case statements 6. Through the conditional macro definition, the same function name has different implementations, which is convenient to call this function #ifdef xxx #define func(x) do{implementation;}while(0) #else #define func(x) do {}while(0) 7. Zero-length arrays GNU C allows the use of zero-length arrays, which is very useful when defining the header structure of variable-length objects. For example: struct minix_dir_entry { __u16 inode; char name[0]; }; 8. Variadic macros In GNU C, macros can accept a variable number of arguments, just like functions, for example: #define pr_debug(fmt,arg. ..) \ printk(KERN_DEBUG fmt,##arg)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326415904&siteId=291194637