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

I was looking through some code and I came across this macro definition

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

And used in this code,

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

I ran the code and this particular line does nothing. So why does someone define such a macro?

If you're interested, this macro is defined in the _pam_macros.h header file.

+3
source share
2 answers

, D , #ifdef , , , , . do/while , , D(...);, D(...) (. )

+8

, a; , .

( ) :

#define DBG(x,y) printf ("Debug output %d\n", x); printf ("  %d\n", x+y); 

:

void someFun ()
{
  DBG(10,0)
  DBG(11,40)
}

.

Do {...} while (0) ; , , , . , . , , :

#define DBG(x,y)                   \
do                                 \
{                                  \
  printf ("Debug output %d\n", x); \
  printf ("  %d\n", x+y);          \
} while (0)

a; . , .

- , , :

if (x==y)
  DBG(x,y);

x y, , if, .

, , :

#define DBG(x,y) {printf ("Debug output %d\n", x); printf ("  %d\n", x+y);} 

, , ";" .

, , , , . , 5 , , - , , , , , .

, .

0

Source: https://habr.com/ru/post/1752848/


All Articles