John Doh John Doh - 4 months ago 18
C Question

Why would someone define a C macro with unused arguments / parameters?

I was browsing some code and I came across this macro definition

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


And its used in the code like this,

D(("couldn't identify user %s", user));


I ran the code, and that particular line doesn't do anything. So, why would some one define a macro like that?

In case you're wondering, that macro is defined in the _pam_macros.h header file.

Answer

Most likely D is for debugging, and there's an #ifdef elsewhere that makes it do something more useful if debugging is enabled, like output the message or log it to a file. The do/while loop is to make it require a semi-colon at the end, so the user can call it as D(...); instead of just D(...) (see this post)