Why is this C program giving unexpected results?

Possible duplicate:
C programming: is this behavior undefined?

#include<stdio.h>
main()
{
 int i=5;
 printf("%d%d%d",i,i++,++i);
}

my expected result is 556. But when I completed it, the result is 767. how?

+3
source share
4 answers

You cannot be sure that the increments are executed in the expected order, because the instructions inside the arguments are executed in the order chosen by your compiler.

+1
source

You gain access and change the value to a point in the sequence (changing it twice, infact), within the point of the sequence you cannot be sure that the order of operations.

. , , . i , 5. ++, 6 , ++i, i ..

+1

, , . :

int i, j, k;
i=j=k=5;
printf("%i%i%i",i,j++,++k);

, . , , .

0
$ gcc -Wall arst.c  
arst.c:2:1: warning: return type defaults to β€˜int’

arst.c: In function β€˜main’:

arst.c:5:27: warning: operation on β€˜i’ may be undefined

arst.c:5:27: warning: operation on β€˜i’ may be undefined

arst.c:6:1: warning: control reaches end of non-void function

.

-1

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


All Articles