6

In the following code

int main(){  
    int a=3;  
    printf("%d %d %d",++a,a,a++);
    return 0;
}  

As specified, From C99 appendix C:,

The following are the sequence points described in 5.1.2.3:

  • The call to a function, after the arguments have been evaluated (6.5.2.2).
  • The end of the first operand of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17)

The order in which the arguments to a function are evaluated are undefined as specified by the C standard.

However, in the function call for printf, we have arguments that are separated by commas which classify as sequence points. So why does this statement correspond to unspecified behavior?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
kauray
  • 739
  • 2
  • 12
  • 28
  • 1
    To be clear: those commas in the `printf` call are *not* sequence points, and they most certainly do not confer any well-definedness onto the obviously-undefined combination of `a++` and `++a`. – Steve Summit Jan 22 '16 at 17:13

1 Answers1

13

Because the comma in the function call is not the comma operator but a separator. So it doesn't introduce any sequence point(s).

P.P
  • 117,907
  • 20
  • 175
  • 238
  • When does the comma operator behave as a separator and when does it behave as a sequence point? – kauray Jan 22 '16 at 16:15
  • 2
    @kevin, e.g. separator `int x[] = {1,2,3}`, `call(1,2,3)`; sequence point: `int x[] = {(1,/*comma op*/ 2), /*separator*/ 3}`, `call((1,/*comma op*/ 2),/*separator*/ 3)`. – Kijewski Jan 22 '16 at 16:17
  • 4
    @kevin The comma doesn't "behave". It is a symbol which *occurs* in a syntax rule. When you use the function call syntax, the commas between the arguments are punctuator tokens which separate. In a comma expression, the comma denotes an operator. So it depends on which syntax rule is obeyed by the thing that you're writing. – Kaz Jan 22 '16 at 16:19
  • 6
    In other words, the language spec allows *comma* to have different behaviours. You can also ask ((and many similar questions) why the *multiplication operator* `*` is used to dereference a pointer (e.g.`*p=5;` :) – P.P Jan 22 '16 at 16:21