please explain the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

please explain the output?

int b=3; b=b++; cout<<b; the output is still 3!!

26th Nov 2016, 5:04 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
14 Answers
+ 3
Well after a long argument I finally got the answer that this (multiple increment /decrement operator) is compiler dependent.
15th Dec 2016, 10:33 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 2
thanks for your answer @ali kamyanfar
29th Nov 2016, 6:45 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 1
++b icreases the value in first iteration. whereas b++ increase value by 1 but can be used in next time.or next step only.
26th Nov 2016, 5:30 PM
Rohan Shrestha
Rohan Shrestha - avatar
+ 1
sir...maybe...first it is incremented to 4 then add to 4...total 8...but there is post increment..that is why it get incremented to 9...++I(4 pre)+I++(still 4 )=8...but at the last there was post increment which still didn't get incremented..so after incrementation 9
27th Nov 2016, 3:24 AM
somnath
somnath - avatar
+ 1
thanks for your answer Aditya kumar pandey
27th Nov 2016, 9:51 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 1
for example int x=3; int x=x++;/* before 3 change to 4 and 1 add to x in x++ 3 move to x so x still is 3 */ x=++x;/* first 1 add to x in ++x then move to x so x=4 */ cout<<x; //out put is : 4 @Rishabh Agrawal @sagar
29th Nov 2016, 5:28 PM
Ali Kamyanfar
Ali Kamyanfar - avatar
0
At first line value of b is assign to 3. On second line it is b++. What does it mean. Increase value of a variable by 1 but if it used it again. Means if It use again its value increase. Now you assign b++ value to b. So, what is value of b now its 3. On 4th line its value is output. What is value of b. Is it 4, no. It is 3. because you assign value of b++ to b and here b is used again but it already assign value to 3. Now after that there is nothing like increment. If b++; was written instead of b=b++. Than its output will 4.
26th Nov 2016, 5:13 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
b=b++ does three things. Expression evaluation, increment, and assignment. In which order? Since it is a post-increment, the increment is executed after the expression evaluation (thus b is assigned the original value of b, that is 3). In principle the increment could be performed also after the assignment, but as this contrived example shows that is not the case (otherwise b would become 4). Hence the order of operations is expression evaluation, increment, and assignment. EDIT I tested the code in the code playground section of the C++ course and the output is actually 3, so unless it is not working properly the OP appears to be mistaken. Revising my reasoning, the order of operations is evaluation, assignment, and increment.
26th Nov 2016, 6:27 PM
Giulio Pellitta
Giulio Pellitta - avatar
0
OutPut in your questions is wrong..that should be 4...
27th Nov 2016, 3:27 AM
somnath
somnath - avatar
0
its value is 4 but print 3 then add 1 to b . but in ++b its value is 4 and print 4.
29th Nov 2016, 1:15 PM
Ali Kamyanfar
Ali Kamyanfar - avatar
0
that means if i display b again it will show 4. @Ali kamyanfar
29th Nov 2016, 1:44 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
0
@Ali kamyanfar can u explain me what is exactly happening at 2 nd like of code as by setting the code, the variable is being incremented twice but output is only after 1 increment. It will be good if u could explain it to me in detail.
29th Nov 2016, 9:35 PM
Sagar
Sagar - avatar
0
int main(){ int b=3; b=b++;/*before 3 change to 4 move to b so b still is 3 */ b=b++; //b still is 3 b=b++; //b still is 3 b =++b; /* b is 4 now because 3 change to 4 then move to b */ cout <<b; //out put is 4 }
30th Nov 2016, 3:15 PM
Ali Kamyanfar
Ali Kamyanfar - avatar
- 1
in the program the value assigned to 'b' is 3 and then the 3 is incremented to 4, but the value now stored in b remains 3 to change the value of 'b' from 3 to 4, two things can be done 1. b=++b: 2. b++=b:
26th Nov 2016, 6:51 PM
Abhinav Kumar
Abhinav Kumar - avatar