Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<bits/stdc++.h>
using namespace std;

int main()
{
int i = 5;

int ans = (i++) *(++i);

cout << ans << endl;

return 0;
}

This code gives output 35 and i am not able to understand what is happening there

What I have tried:

nothing i can try in this, i cant find the solution for this
Posted
Updated 10-Jul-18 20:19pm
Comments
Mehdi Gholam 11-Jul-18 1:44am    
35 = 5 * 7, work it out.
Member 13906667 11-Jul-18 2:15am    
how???

Basically, don't do that!
See here: Why does x = ++x + x++ give me the wrong answer?[^]
 
Share this answer
 
Works out to:

int ans = i * ((i+1)+1);

The first "i" is 5; the "second reference" to "i" gets i++ and then ++i (7) before "*" is evaluated.

Order matters; otherwise you get 36.
 
Share this answer
 
Comments
Member 13906667 11-Jul-18 2:41am    
what about (++i)*(i++)
[no name] 11-Jul-18 11:02am    
What about it?

I said "order matters", and in that case the answer is 36.

(i++ is "last"; so its (6 * 6); with i ending up as 7 (again); but after "ans" is calculated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900