C in gcc (prefix / postfix operator and priority)

I have a small C code:

#include<stdio.h> int main() { int z[]= {1,2,3,4,5,6}; int i = 2, j; printf("i=%d \n",i); z[i] = i++; for (j=0;j < 6;j++ ) printf ("%d ", z[j]); printf("\ni=%d \n",i); } 

output:

 i=2 1 2 2 4 5 6 i=3 

Priority order for evaluating an expression First, z [i] is evaluated. Since I am here 2, it becomes z [2]. Then I ++ evaluates to ie 2, and I becomes 3. Finally, = is executed, and 2 (ie, the value obtained from i ++) is placed in z [2]

This explains the above conclusion ie 1 2 2 4 5 6

But if we change the above code from i ++ to ++ i i.e.

 #include<stdio.h> int main() { int z[]= {1,2,3,4,5,6}; int i = 2, j; printf("i=%d \n",i); z[i] = ++i; for (j=0;j < 6;j++ ) printf ("%d ", z[j]); printf("\ni=%d \n",i); } 

Then the conclusion is strangely different, namely:

 i=2 1 2 3 3 5 6 i=3 

if we go to the above priority (that C spec says that [index] are linked earlier than ++), then the output should have been 1 2 3 4 5 6.

I just want to know why this priority order doesn't explain this?

my compiler is gcc 4.5.2 on ubuntu 11.04

Thanks and Regards, Kapil

+4
source share
5 answers

z[i] = ++i; leads to undefined behavior:

6.5 Expressions
...
2 If the side effect of a scalar object is independent of another side effect for the same scalar object or calculating a value using the value of the same scalar object, the behavior is undefined. If there are several valid orders of expression subexpression, the behavior is undefined, if such an inconsistent side of the effect occurs in any of the orders. 84)
84) This paragraph displays undefined expression expressions such as
 i = ++i + 1; a[i++] = i; 
letting
 i = i + 1; a[i] = i; 

Note that priority only controls the grouping of operators and operands; he does not control the evaluation procedure.

The side effect of the ++ operator in ++i is independent of the [] operator in z[i] ; the compiler is not required to evaluate the two expressions in any particular order. Also note that the side effect of ++i should not be applied immediately after evaluating the expression; it should be applied only to the next point in the sequence.

+3
source

This is not a bug, see the GCC non-bug section. Such tests will lead to unpredictable results, and they know it.

Changing a value twice between two points in a sequence [...] results in undefined behavior.

+2
source
  z[i] = ++i; 

- undefined behavior

Quote from section C cppreference.com

1) If the side effect of a scalar object is independent of another side effect on the same scalar object, the behavior is undefined.

i = ++ i + i ++; // undefined behavior

+2
source

z [i] = ++ i;

causes undefined behavior.

0
source

See http://www.faqs.org/faqs/C-faq/faq/ Questions 3.1 - 3.9. These are answers to all questions similar to your question.

But, in short, the evaluation order between two points in a sequence (they are well defined by the C standard - Appendix C99 C) is undefined behavior.

Between two points in a sequence, an object changes more than once or changes, and the previous value is read differently than to determine the value to be saved.

0
source

Source: https://habr.com/ru/post/1480905/


All Articles