"##" in printk, which means ##

#define ext4_debug(f, a...) \ do { \ printk(KERN_DEBUG "EXT4-fs DEBUG (%s, %d): %s:", \ __FILE__, __LINE__, __func__); \ printk(KERN_DEBUG f, ## a); \ } while (0) 

what i don't understand is

 printk(KERN_DEBUG f, ## a); 

Can someone help me understand what ## is on this line? thanks

+6
source share
2 answers

Here you need to make a variable macro (a macro that can take several arguments) if you pass 0 arguments.

From the Variadic Macros section of the GCC Guide:

Secondly, the # paste token operator has special meaning when placed between a comma and a variable argument. If you write

 #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__) 

and the variable argument is not used when the eprintf macro is eprintf , then the comma before ## will be removed. This does not happen if you pass an empty argument, and it does not happen if the token preceding ## is something other than a comma.

 eprintf ("success!\n") ==> fprintf(stderr, "success!\n"); 

If you have not used this, then it will expand to frpintf(stderr, "success!\n",) , which is a syntax error.

+2
source

This is the token for variable arrays (macros with several variable arguments). Its special gcc directive that allows 0 or more arguments as input after f in ext4_debug() . This means that the argument f is required, a may or may not exist.

This is the same as printf(const char *fmt,...) , where fmt is required, other arguments are optional and depend on fmt . See the last expression in this document: http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

+3
source

All Articles