Increment in C Programming Language | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

24th Feb 2020, 7:11 PM
Dolan
Dolan - avatar
5 Answers
+ 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
24th Feb 2020, 7:36 PM
andriy kan
andriy kan - avatar
+ 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
24th Feb 2020, 7:25 PM
andriy kan
andriy kan - avatar
+ 1
@Dolan Hêriş, you are welcome
24th Feb 2020, 9:26 PM
andriy kan
andriy kan - avatar
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
24th Feb 2020, 7:27 PM
Dolan
Dolan - avatar
0
thank you my friend. andriy kan
24th Feb 2020, 9:20 PM
Dolan
Dolan - avatar