0
Increment in C Programming Language
why the result is not what it should be in follow example: https://code.sololearn.com/coXVMi3i87fh/#c #include <stdio.h> int main(){ int x, y, m; x = 10; y = 15; // y++; m = ++x + y++; printf("x is %d", x); printf("\ny is %d", y); printf("\nResult is %d", m); printf("\nx is %d", x); printf("\ny is %d", y); return 0; } // OUTPUT // x is 11 // y is 16 // Result is 26 // x is 11 // y is 16
5 Respostas
+ 2
@Dolan Hêriş
no,
++x is preincrement, which means that x will be incremented before statement m = ++x + y++;
y++ is postincrement, which means that y will be incremented after statement m = ++x + y++;
again, m = ++x + y++; will be executed in the follow steps:
1. x is incremented by 1 (as it is preincrement) x = x + 1 = 11
2. addition of x and y (x was incremented = 11, but y was not incremented = 15) m = 11 + 15 = 26
3. y is incremented by 1 (as it is postincrement) y = y + 1 = 16
+ 2
The result is exactly what it should be
You can write statement m = ++x + y++; as follows:
x = x + 1; // x = 10 + 1 = 11
m = x + y; // m = 11 + 15 = 26
y = y + 1; // y = 15 + 1 = 16
+ 1
@Dolan Hêriş, you are welcome
0
andriy kan Thanks. the (y++ = 16). the (x++ = 11), so the result supposed to be 27.
why one added to the variable X but not added to the Y?
m = ++x + y++
m = 11 + 16 —> 27
0
thank you my friend. andriy kan