-3
printf( "%3o\t%2x\t%3d\t%c\n", c = getchar(), c, c, c );

I'm getting a warning that says "unsequenced modification and access to 'c' [-Wunsequenced]". The error is fairly easy to fix; all I have to do is separate getchar() from printf(), but I just want to have a better understanding of why this instruction is producing a warning, and what would go wrong if I actually ran it.

Does this have anything to do with the implementation of printf() using CPP macros for the variable-length argument list?

user628544
  • 227
  • 3
  • 9
  • 1
    Possible duplicate of [Why are these constructs (using ++) undefined behavior?](http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior) – too honest for this site Nov 12 '16 at 18:59

1 Answers1

0

The order in which arguments passed to a function call are evaluated is unspecified. There is no guarantee that the result of getchar() will be assigned to c before the last three c arguments are read.

Calling c = getchar() outside of the function call fixes the issue.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416