can anyone give me solution of this code that why it shows output 6 7 7 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can anyone give me solution of this code that why it shows output 6 7 7

#include<iostream>using namespace std;int main(){int i =5;cout<<i++<<++i<<i;return 0;}

4th Sep 2016, 9:08 PM
Shivesh Karan Mehta
Shivesh Karan Mehta - avatar
5 Answers
+ 1
1. 5 ---> i++ --> prints 6 2. 6----> ++i--> prints 7 3. 7---->you don't add anything ---> prints 7 Also keep in mind: The difference is that the post-increment operator i++ returns i as it was before incrementing, and the pre-increment operator ++i returns i as it is after incrementing
4th Sep 2016, 9:17 PM
Benas
0
can you please explain me line by line?
4th Sep 2016, 9:13 PM
Shivesh Karan Mehta
Shivesh Karan Mehta - avatar
0
Ok. So you have integer int i = 5. Then you have cout<<i++(at this point it takes 5 and adds +1 so it prints 6)<++i(at this point it adds up +1 to 6, so it prints 7)<i;(you don't change here anything. i=7)
4th Sep 2016, 9:25 PM
Benas
0
but in post increment first write the value and then its value increase by 1..so that according my ans is 5 7 7...if i am wrong than correct me
4th Sep 2016, 9:26 PM
Shivesh Karan Mehta
Shivesh Karan Mehta - avatar
- 1
sorry but i didn't get you
4th Sep 2016, 9:13 PM
Shivesh Karan Mehta
Shivesh Karan Mehta - avatar