Ex19 - confused by the debug macros, help!


I am hoping someone can help. I’m trying to get my head around the macros in Ex19 of the book.

#define debug(M, …) fprintf(stderr, "DEBUG %s:%d: " M “\n”, FILE, LINE, ##VA_ARGS)

I understand that this is called a ‘function like’ macro where you are effectively defining the function that preprocessor will use to replace the identifier ‘debug’. I also understand that … and ##VA_ARGS work together to allow you to concatenate multiple arguments and pass them to fprintf to print them to the relevant stream. But what is confusing me is the M parameter and the use of M in between the "DEBUG %s:%d: " string and newline “\n”. I thought at first that the M was almost to just make sure the pre processor was aware there is a parameter that comes before the variable arguments as in the way stderr is passed as the first argument. But, that didn’t explain why the M came between the two strings. This is really bugging me and for some reason nothing I read is helping to clear it up. Is anyone able to help?

I’ve realised I was being really silly, the M just gets expanded to include the string you pass in… derp.

Yep! It’s super weird but that’s C for ya.