Word limit on question length..
As pointed out by @Karl Knechtel I am confused that isn't fetching the operation of the array indexing unsequenced relative to the i++
increment operation? If they are unsequenced, why the C Standard 6.5.2 line mentioning about (emphasis added to the words/phrase which i understand, applies here)
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
I read this question I can not understand some sentences in C99 wherein the OP tries to understand why a[i++] = 1
is undefined. Accepted and one of the highest voted answers by Pascal Cuoq mentions that this is defined behavior.
I also tried compiling the program using the -std=c99
, -Wall
and -Wextra
flag and a slew of other flags (basically all the flags which are enabled in GCC 11.2.0), but the code didn't throw any warning.
However, my question/confusion is why is this a defined behaviour?
From the C11 standard S6.5.2
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behaviour is undefined if such an unsequenced side effect occurs in any of the orderings.
my understanding/reasoning after reading through most of the threads on SO (with Tags [C]
and [sequence-points]
) is that i++ would result in a side effect of updating the value of i. in that case this side-effect is unsequenced to the value computation using the same scaler object. I understand that a[integer object]
constitutes value computation
. Then, it should be undefined behavior?
Even from the C99 S6.5(p2)
Furthermore, the prior value shall be read only to determine the value to be stored.
I understand/construe that this expression should also render a[i++] = 1
undefined?