DIGIT-ARC read-only archive · snapshot: July 23, 2026

Temporary C++ amnesia 8|

8 posts · originally from https://geek.digit.in/community/threads/temporary-c-amnesia-8.5452/

D
DKant Original post #1
Feb 6, 2005 at 8:58 PM
void main()
{
int i=5;

i=(++i)/(i++);

cout<<i;
}

Simple one. But the result I expected was 1 while the actual thing is 2. So what exactly am I forgetting here? :shock:
NikhilVerma
Feb 6, 2005 at 10:17 PM
This is simple....
Do you see the i++ part...

it's like this ++i makes the value of i 6
then iafter the division make the division of 6 with 6 and you get one...
which is stored in i
then the ++ makes it two....

Tell me if you didn't understand....
S
Feb 6, 2005 at 10:17 PM
The answer is simple
i=(++i)/(i++)
First, ++i => i=6
Then, i++=> use six, and AT THE END OF THIS STATEMENT, ADD 1 to i
i=6/6=1
NOW, add 1 to i =>i=2

Hope that helps...
Arun
NikhilVerma
Feb 6, 2005 at 10:18 PM
Dang the odds !!!!!

We made replies at the exact same time !!!
and in the similar fashion!!!
harmax
harmax #5
Feb 6, 2005 at 10:29 PM
whatever the value of i the answer is 2




obviously
D
DKant #6
Feb 7, 2005 at 5:39 PM
Ahh! That's what I forgot. i++. Thanx a ton m8! :)
harmax
harmax #7
Feb 7, 2005 at 10:12 PM
how come the division operator has higher precedence than '(' ?
S
Feb 7, 2005 at 10:28 PM
Hi Nikhil,
It sure was a coincidence... What are the odds, 2 people posting the answer at the same time and in similar manner!!!

Harmax,
If you read my post or Nikhil's post carefully, and rework with any value of i manually, u will see the reason. It is not that division has higher priority over '(', but that it has higher priority over post-increment operator.

DKant,
Glad we could help...

Arun