+ 3

Doubt

Someone pls explain how this comes...?? int x,y,z; x=2; y=5; z=x+++y; printf("%d %d %d",x,y,z); output: 3 5 7

3rd Jun 2018, 6:53 AM
Thameem Jabir KJ
Thameem Jabir KJ - avatar
6 Respuestas
+ 9
The line z= x+++y; is evaluated as z=x++ + y; where : x++ is post increment of x , so its value changes only after the sum of x+y is evaluated. This is equivalent to: x=2; y=5; z=x+y; x=x+1;
3rd Jun 2018, 7:06 AM
ifl
ifl - avatar
+ 8
At very first the addition is performed and then X is updated to 3 (X++) before updating X ,Z is assigned 7 .And that's all .🤗
3rd Jun 2018, 8:40 AM
Ananya Srivastava
Ananya Srivastava - avatar
+ 6
See this article: https://www.sololearn.com/learn/Java/2141/ Z gets the value of x+y (2+5=7), and the x++ increments the value of x, making it 3. Fun fact: doing z=++x+y; would return 8, as it does the calculation before the assignment. Link explains it better.
3rd Jun 2018, 7:04 AM
Andre Daniel
Andre Daniel - avatar
+ 2
ok friends.....i understood what it is.......
3rd Jun 2018, 4:07 PM
Thameem Jabir KJ
Thameem Jabir KJ - avatar
+ 1
btw. some examples of post and pre increment a here :-) https://code.sololearn.com/ceYh2erq9U7y/?ref=app
3rd Jun 2018, 1:51 PM
ifl
ifl - avatar
+ 1
left to right so it is considers x++ +y since it is post increment at the time it will be 2, then incremented to 3 z=2++ +5 gives 7 so x get incremented to 3; hence result 3 5 7
4th Jun 2018, 12:38 PM
Tamilselvan.T
Tamilselvan.T - avatar