Behavior of Increment Operators

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

1
cout<<a++<<a<<++a; 


Moderator's Note: This kind of questions actually have no answer. Here’s why. In practice, avoid them

Note by Anweshan Bor
3 years, 6 months ago

No vote yet
1 vote

  Easy Math Editor

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

  • Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
  • Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
  • Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 2×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \boxed{123}

Comments

Sort by:

Top Newest

The output should be 789 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=7a = 7 gets printed then its value gets incremented to 88. After this when you use the ++a operator, it first increments the value of aa 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 :)

neelesh vij - 3 years, 6 months ago

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.

anweshan bor - 3 years, 6 months ago

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.

Vishnu Bhagyanath - 3 years, 6 months ago

Log in to reply

@Vishnu Bhagyanath Well, it's worse than a bad idea. It is undefined.

Now that I am an ex-student, I should probably drop an email to our CS teacher.

Agnishom Chattopadhyay Staff - 3 years, 6 months ago

Log in to reply

@Agnishom Chattopadhyay I asked this from my CS teacher and she said that the output depended on which compiler we are using. Different compilers could give different output. Is it so??

anweshan bor - 3 years, 6 months ago

Log in to reply

@Anweshan Bor Read up on the link Agnishom posted

Vishnu Bhagyanath - 3 years, 6 months ago

Log in to reply

@Vishnu Bhagyanath Actually the cascading took place from right to left as after postfix(++a) .It becomes the last updated value i.e 8 . So compiler starts reading the output from right to left.

tivansh verma - 3 years, 6 months ago

Log in to reply

@Agnishom Chattopadhyay Please cc to all Indian CS teachers.

Pranjal Jain - 3 years, 3 months ago

Log in to reply

@Pranjal Jain Haha

Agnishom Chattopadhyay Staff - 3 years, 3 months ago

Log in to reply

Actually, coding like this is ambiguous and considered a bad idea

Agnishom Chattopadhyay Staff - 3 years, 6 months ago

Log in to reply

Yes totally agreed.

neelesh vij - 3 years, 6 months ago

Log in to reply

Answers explaining why this is not a good idea Quora and StackOverflow

@anweshan bor

Agnishom Chattopadhyay Staff - 3 years, 6 months ago

Log in to reply

The answer is : 7 8 8

Rashi Kashyap - 3 years, 6 months ago

Log in to reply

Answer will be 888.

tivansh verma - 3 years, 6 months ago

Log in to reply

You completely ignored what I said.

Agnishom Chattopadhyay Staff - 3 years, 6 months ago

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...

Who Am I - 3 years, 6 months ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...