x -= 1; // 2 x /= 2; // 3 x %= 2; // 1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

x -= 1; // 2 x /= 2; // 3 x %= 2; // 1

Why did they happen like that İf we think like normal math, for the first example 2+1=1 should be like that Second example : 2/2=1 should be like that and Thirth example : 2%2=0 but why the Outputs are 2,3,1

16th Nov 2018, 6:22 PM
Esra Gül Anlar
Esra Gül Anlar - avatar
2 Answers
+ 5
The compound assignment operators are shortcuts. This: x = x + (expression); can be shorten to: x += expression; It means calculate x + (expression) and replace the current value of x with the result.
16th Nov 2018, 8:18 PM
John Wells
John Wells - avatar
0
Thank you for your answer. Actually I was talking about the example which is given The whole code was: #include <stdio.h> int main() { int x = 2; x += 1; // 3 x -= 1; // 2 x *= 3; // 6 x /= 2; // 3 x %= 2; // 1 x += 3 * 2; // 7 printf("%d", x); return 0; } and I did not understand examples I mentioned above
16th Nov 2018, 8:03 PM
Esra Gül Anlar
Esra Gül Anlar - avatar