2

I think that I saw somewhere that writing more than 1 instruction separated by a comma , is undefined behavior.

So does the following code generate undefined behavior?

for (i=0, j=3, k=1; i<3 && j<9 && k<5; i++, j++, k++) {
    printf("%d %d %d\n", i, j, k);
}

because there are 3 instructions separated by a comma , :

i++, j++, k++
Simon Gibbons
  • 6,969
  • 1
  • 21
  • 34
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 6
    Nope, that isn't undefined. Why do you think it should be? It's only using and updating one variable in each sequence-point. – Mats Petersson Jul 30 '15 at 08:11
  • 1
    @MatsPetersson I think that `i++, i+=5` in the same sequence point is not valid – MOHAMED Jul 30 '15 at 08:15
  • Yes it is. The comma operator introduces a sequence point so it is valid. – Filipe Gonçalves Jul 30 '15 at 08:22
  • @MOHAMED: You will have problem with`foo(i++, i+=5)`. Here, the coma is not the operator but a argument separator. – Jarod42 Jul 30 '15 at 08:26
  • ..but because arguments are pushed right-to-left (cdecl), first 5 will be added to i and the result pushed, then i is pushed and then i is incremented. I believe this is then well defined. – Paul Ogilvie Jul 30 '15 at 08:44

2 Answers2

6

writing more than 1 instruction separated by comma , is undefined behaviour.

Nope, it's not the general case.

In your case, i++, j++, k++ is perfectly valid.

FWIW, as per C11, chapter §6.5.17, Comma operator (emphasis mine)

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; [...]


[Note]: You might have got confused by seeing something along the line of

  printf("%d %d %d", i++, ++i, i);

kind of statement, but do note, there the , is not a comma operator altogether (rather, a separator for supplied arguments) and the sequencing does not happen. So, those kind of statements are UB.

Again, referring to the standard, footnote 3 for the same chapter

As indicated by the syntax, the comma operator (as described in this subclause) cannot appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists of initializers).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I think that `i++, i+=5` in the same sequence point is not valid – MOHAMED Jul 30 '15 at 08:15
  • 2
    @MOHAMED nope, it's valid. see the emphasis on the quote. – Sourav Ghosh Jul 30 '15 at 08:16
  • does the both expression are valid in the C99? – MOHAMED Jul 30 '15 at 08:18
  • @MOHAMED Yep, absolutely. – Sourav Ghosh Jul 30 '15 at 08:19
  • In the `printf` using cdecl, wouldn't first i be pushed, then i incremented and pushed and then i pushed and incremented? Taking i=5 as an example, it would be `push i=5, inc i -->6, push i=6, push 6, inc i-->7`? – Paul Ogilvie Jul 30 '15 at 08:48
  • 1
    @PaulOgilvie No, you're mistaken sir. When the comma is used as separator, there is no sequence point, and having more than one _un-sequenced_ side effect is declared UB by the standard. See my updated answer. :-) – Sourav Ghosh Jul 30 '15 at 09:13
  • @Sourav-Gosh, your quote does not say that the comma used to separate arguments is not a sequence point. Your quote only says that if the comma is, by the syntax, used as a separator, the comma operator cannot be used. – Paul Ogilvie Jul 30 '15 at 11:38
  • @PaulOgilvie Yes, that means the properties of comma operator is also not applicable there, right? – Sourav Ghosh Jul 30 '15 at 11:39
  • @Sorav-Gosh, the standard says Indeed: "_The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified_". See also http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c, which shows I am hopelessly wrong... – Paul Ogilvie Jul 30 '15 at 12:05
  • @PaulOgilvie Let me say I was too lazy to do a search for the answers I knew existed. :-) Being wrong is not bad, staying wrong is. Hope it clarified your doubts. and, (just kidding), my name is So***u***rav G***h***osh... – Sourav Ghosh Jul 30 '15 at 12:13
2

Your example is perfectly fine C code.

There are instances where the comma has a different meaning, for example in declaration statements. In declaration statements, comma is used to separate the declaration of several variables.

int a;
a = 1,2,3;  // Ok. a is assigned the value 3.

int a = 1,2,3;   // Not ok! 
int a = 1, b = 2; // Ok! a is assigned the value 1.
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 2
    Not every comma is a "comma operator"... the commas in `a = 1,2,3;` are, but the others you show aren't (and neither are the ones in array initializers, or the ones separating function arguments). – Dmitri Jul 30 '15 at 08:27
  • @Dmitri Point taken. The word operator stricken when referring to declarations. Also removed false claim that comma is used only in one other instance. – Klas Lindbäck Jul 30 '15 at 08:49