0

Please solve and send me explanation.

When i gave my coding test i got this question and tried all the way i know but can't understand this question. If anyone can solve this then send me its explanation. The question is #include <stdio.h> int main() { int x=2; int y=5; x+=1; y-=x; y++; printf("%d",y); return 0; }

9th Jul 2024, 3:44 AM
satynarayan suthar
satynarayan suthar - avatar
6 Réponses
+ 7
go over it line by line. write what's the variable's expected value should be after each operation. x+= 1; // x= 3 y-=x: // y = 5-3 = ? y++; // y = ?
9th Jul 2024, 4:27 AM
Bob_Li
Bob_Li - avatar
+ 6
it's not even a question. you can just run the code to get the answer. The purpose is to understand how it is obtained. int x = 2; int y = 5; x+=1 means add 1 to the value of x x+= 1; // x= 2+1 = 3 (x was 2, now it's 3) y-=x means subtract x from y y-=x: // y = 5-3 = 2 (y was 5, now it's 2) y++ means add 1 to y. it's the same as y+=1 y++; // y = 2 +1 = 3 // so what's the final value of y? 3. printf("%d", y); //prints 3
9th Jul 2024, 12:10 PM
Bob_Li
Bob_Li - avatar
+ 5
John Greals , having a look to your `codebits` makes me believe that you should be able to understand what your mentioned code is doing and how it does work.
9th Jul 2024, 3:37 PM
Lothar
Lothar - avatar
+ 4
As Bob_Li said, go over it line by line - you could/should put a print after every statement and see what's happening.
9th Jul 2024, 1:36 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
What is your expected output? If you can explain how to get your expected output, then we can discuss in details how it is correct or wrong.
9th Jul 2024, 4:17 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
I don't know
9th Jul 2024, 8:27 AM
satynarayan suthar
satynarayan suthar - avatar