+ 1
Could you explain the output of this code. ?
#include <stdio.h> int main() { int x=10; printf("%d \n",x + x++ ); x=10; printf("%d \n",x + x++ + x++); x=10; printf("%d \n",x + x++ + x); x=10; printf("%d \n",x + x++ + x+ x); x=10; printf("%d \n",x + x++ + x++ +x); x=10; printf("%d \n",x + x++ + x++ +x++); return 0; } Output: 21 32 32 43 44 44
2 Antworten
+ 1
printf("%d \n",x + x++ );
// x = 10, x++ = 11 so sum is 21. (10+(10+1) = 21)
x=10;
printf("%d \n",x + x++ + x++);
// x = 10, x++ = 11 and , sum is 32. You may think why sum isn't 33, cause you add x first after you increment its value. (10+(10+1)+11 = 32) + 1 but after printing so it is not calculated while it displays.
x=10;
printf("%d \n",x + x++ + x);
// x = 10, x++ = 11 and x = 11 again, sum is 32. This statement the same with previous statement.(11+(10+1)+11 = 32)
x=10;
printf("%d \n",x + x++ + x+ x);
// x = 10, x++ = 11 now and the final value of x is 11 so sum is 43. (10+(10+1)+11+11 = 43)
x=10;
printf("%d \n",x + x++ + x++ +x);
// x = 10, x++ = 11 now and final value of x is 12. So, sum is 44. (10+(10+1)+(11)+12 = 44)
x=10;
printf("%d \n",x + x++ + x++ +x++);
// x = 10, x++ = 11 and x++= 12. (10+(10+1)+(11+1)+12 = 44) +1 but after prints this statement so it is not calculated.
0
In that case value are not changing in last two statements but why?
x and x++ are showing the same result.