Please guide me what'll be output of the following code segment in c++ if 'a' is initialized at 7.
1 

Moderator's Note: This kind of questions actually have no answer. Here’s why. In practice, avoid them
Problem Loading...
Note Loading...
Set Loading...
Easy Math Editor
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
2 \times 3
2^{34}
a_{i1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
Sort by:
Top NewestThe output should be \( 7 \quad 8 \quad 9\)
When you use a++ operator it first prints the value of a then increments its value. so first the value of \(a = 7\) gets printed then its value gets incremented to \(8\). After this when you use the ++a operator, it first increments the value of \(a\) then prints it so first the value to a is increased to 9 then it is printed.
In short the post increment operator (a++) increments the value of a after the command given to it while the pre increment operator (++a) first increments the value of a then it is used in the command given to it.
Hope it helps :)
Log in to reply
I too thought of the same output but my compiler gave it out as '888'. Well, thank you for the views.
Log in to reply
Cascading cout statements are initialized from right to left. Though as Agnishom mentioned, this isn't a good idea, such questions commonly come for Viva.
Log in to reply
Now that I am an exstudent, I should probably drop an email to our CS teacher.
Log in to reply
Log in to reply
Log in to reply
Log in to reply
Log in to reply
Log in to reply
Actually, coding like this is ambiguous and considered a bad idea
Log in to reply
Yes totally agreed.
Log in to reply
Answers explaining why this is not a good idea Quora and StackOverflow
@anweshan bor
Log in to reply
The answer is : 7 8 8
Log in to reply
Answer will be 888.
Log in to reply
You completely ignored what I said.
Log in to reply
The answer is 888.. Bcoz cout has a right to left associativity... So the answer mostly depends on compailer...in right to left associative answer is 888...
Log in to reply