views:

55

answers:

1

Hi,

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 ?

+6  A: 
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;
}
Roger Pate
tried that, i get the same result .
seven
@seven: are you sure you've taken the inner set of parentheses off?
Mike Dinsdale
sry, i just saw that you hadn't use double brackets, it works , thank you .
seven
When not used in function calls the comma operator works by returning its second operand and has extremely low operator presidence (higher than assignment, though).
nategoose