Please someone expain me how this happens. 😊 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Please someone expain me how this happens. 😊

int p=8; cout <<p++; cout << ++p; //output 810

11th Jun 2017, 4:40 AM
primex
primex - avatar
8 Answers
+ 13
cout << p++; // The value of p is used, before incrementing // 8 is printed // then value of p becomes 9 cout << ++p; // the value of p is incremented, and then used // value of p becomes 10 // 10 is printed // 810
11th Jun 2017, 5:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
1)cout << p++ will display the output of p first and then increase the value of p by 1 so now value of p is 9..and output is 8 2)cout << ++p will increase the value of p first and then then it will display output...so here value of p will be 9+1=10... considering both the steps...the output will show you 810...
11th Jun 2017, 5:05 AM
Prahar pandya
Prahar pandya - avatar
+ 5
@Hatsy Thank you very much. 😊😊😊😊
11th Jun 2017, 5:02 AM
primex
primex - avatar
+ 4
@chirag thank you. yeah. I have some doubts with them.
11th Jun 2017, 5:05 AM
primex
primex - avatar
+ 4
@Prahar thank you😊
11th Jun 2017, 5:06 AM
primex
primex - avatar
+ 4
@pratik thank you👌
14th Jun 2017, 12:09 PM
primex
primex - avatar
+ 1
p++ is prefix. means it print the value of p first and than increment tha value of p. so as shown in your output it print 8 first and than it increment. so now p=9. and in next line of code ++p is post fix so it increment the value of p and than print. so value of p is 9 which increment first and than print so as shown in your output is 10 after 8. 8 and 10.
14th Jun 2017, 11:04 AM
pratik
pratik - avatar