简体   繁体   中英

“##” in printk, what does ## mean

#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 dont understand is this

printk(KERN_DEBUG f, ## a); 

Could anybody help me to understand what is ## in this line? thank you

Its a token for variadic macros(macros with multiple, variable arguments). Its gcc specific directive that allows 0 or more arguments as an input to, after f in ext4_debug() . Which means, f argument is mandatory, a may or maynot exist.

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

It is there to make the variadic macro (macro which can take multiple arguments) work if you pass in 0 arguments.

From the Variadic Macros section in the GCC manual:

Second, the ## token paste operator has a 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 left out when the eprintf macro is used, then the comma before the ## will be deleted. This does not happen if you pass an empty argument, nor does it happen if the token preceding ## is anything other than a comma.

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

If you did not use this, then that would expand to frpintf(stderr, "success!\\n",) , which is a syntax error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM