green
green

Reputation: 117

macro function for printing

if for example i have :

#define PRINT(x) fprintf(stderr, x);

and in code i append it :

 PRINT(("print this")) 

output is : [print this]

if i append it :

 PRINT(("print %s", "this"))

output is : [this]

could someone explain me why it receives just the "this" argument and not the whole string ?

Upvotes: 0

Views: 233

Answers (1)

Roger Pate
Roger Pate

Reputation:

PRINT(("print %s", "this"))

becomes:

fprintf(stderr, ("print %s", "this"));

which is equivalent to:

fprintf(stderr, "this");

However, variadic macros (from C99) work:

#define PRINT(...) fprintf(stderr, __VA_ARGS__)

int main() {
  PRINT("print %s", "this");
  return 0;
}

Upvotes: 6

Related Questions