Does the definition int a = 0, b = a++, c = a++;
have defined behavior in C?
Or almost equivalently, does the comma in an object definition introduce a sequence point as for the comma operator in expressions?
Similar questions have been asked for C++:
- Does `int a = 0, b = a` have undefined behavior?
- Is the comma in a variable list a sequence point?
- C++: variable declaration initialization order
The widely accepted answer for C++ is Yes, it is fully defined per paragraph 8/3 of the C++11 Standard:
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself
Albeit this paragraph only refers to the syntax analysis phase and is not quite precise enough regarding the sequencing of operations at runtime.
What is the situation for the C language? Does the C Standard define the behavior?
A similar question was asked before:
Yet the answer seems to refer specifically to the C11 draft and may not hold for more recent versions of the C Standard as the wording of the informative Annex C has changed since the C11 draft and does not seem fully consistent with the Standard text either.
EDIT: of course such an initializer seems uselessly contorted. I definitely do not condone such programming style and constructions. The question arose from a discussion regarding a trivial definition: int res = 0, a = res;
for which the behavior did not seem fully defined (!). Initializers with side effects are not so uncommon, consider for example this one: int arg1 = pop(), arg2 = pop();
a = 0
,b = 1
andc = 2
ora = b = c = 0
? – Rianona = 2
,b = 0
andc = 1
. – Schaper