Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does comma separators in type definition in C guarantee the order?

Tags:

c

c99

c11

Comma operators have the lowest precedence and left-to-right associativity, so this guarantees the order like:

i = ++j, j = i++;

i will be 2, and then j will be 1 after this statement if i and j are both 0 at first.

However, does comma separators in type definition in C also guarantee the order? Such as

int i = 1, j = ++i;
like image 1000
Kevin Dong Avatar asked Jun 13 '15 04:06

Kevin Dong


1 Answers

Your example with the comma operator, i = ++j, j = i++;, is well-defined because the comma operator is a sequence point.

Precedence/associativity is not enough to guarantee this -- they are different to order-of-evaluation and sequence points. For example, i * 2 + i++ * 3 is undefined because there are no sequence points.


The comma separator between declarators, e.g. int i = 1, j = i++;, is also a sequence point. This is covered by C11 6.7.6/3, C99 6.7.5/3:

A full declarator is a declarator that is not part of another declarator. The end of a full declarator is a sequence point.

So there is a sequence point after i = 1, and this code is well-defined.


However, the comma separator between function arguments f(i, i++) is not a sequence point; so that code causes undefined behaviour.


Note: In C11, the term sequence point was mostly replaced with more complicated sequencing relations in order to clearly specify a threading model, but that does not affect the above discussion.

like image 168
M.M Avatar answered Nov 04 '22 12:11

M.M