# prefix increment operator

Page 1 of 1

## 5 Replies - 2575 Views - Last Post: 02 January 2009 - 07:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=79047&amp;s=b1fe7b609b8501ebd1e2a0b54edb40be&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dan_ram

Reputation: 4
• Posts: 141
• Joined: 15-August 07

# prefix increment operator

Posted 02 January 2009 - 06:39 AM

``` int a = 10;
cout << ++a << '\t' << ++a << '\t'<< ++a;

```

for ++a, the value is to be increased by 1 right, how come my output was:
13 13 13?

how did it become 13??
isn't the statement read from left to right? or the whole statement at a time?
Is This A Good Question/Topic? 0

## Replies To: prefix increment operator

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: prefix increment operator

Posted 02 January 2009 - 07:19 AM

Well, in case of << it works from right to left.

You can get more details here... http://www.cplusplus...ator%3C%3C.html

---EDIT---
And as I can see our Expert - Bench on this topic, you can expect some better explanation for this.

This post has been edited by AmitTheInfinity: 02 January 2009 - 07:20 AM

### #3 Bench

• D.I.C Lover

Reputation: 859
• Posts: 2,343
• Joined: 20-August 07

## Re: prefix increment operator

Posted 02 January 2009 - 07:27 AM

The short answer is Undefined behaviour.

There's no hard rule about when the result of the pre-increment operation is stored within a - so output you're seeing is a direct result of the way your compiler works. its highly likely that your output would vary on different platforms and compilers. On your platform, it looks as if the result of ++a is stored as soon as that operation has finished - the next call to ++a works with that result rather than the original value of a.

You're also mixing in operators which take different levels of precedence in C++'s operator hierarchy (Those nearer the top are always evaluated before those nearer the bottom). Take a look at this reference on operator precedence for more information on why ++a is evaluated before <<
http://www.cpprefere...ator_precedence

in summary - the evaluation order of that expression isn't as trivial as it appears

This post has been edited by Bench: 02 January 2009 - 07:37 AM

### #4 dan_ram

Reputation: 4
• Posts: 141
• Joined: 15-August 07

## Re: prefix increment operator

Posted 02 January 2009 - 07:35 AM

ok....
but the link provided explains it to be only for complex expressions..
but isn't this simple? what is the confusion here for?

This post has been edited by dan_ram: 02 January 2009 - 07:35 AM

### #5 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

## Re: prefix increment operator

Posted 02 January 2009 - 07:41 AM

Like Bench said, it really depends on the compiler that you're using.

If I run this, my output is actually 11 12 13

To me, it looks like your compiler is actually running all operations on that line, then executing it.

So it would be looking at it like this:
```int a = 10;
++a;
++a;
++a;
cout << a;
```

Which compiler are you using?

### #6 dan_ram

Reputation: 4
• Posts: 141
• Joined: 15-August 07

## Re: prefix increment operator

Posted 02 January 2009 - 07:42 AM

quincy 2005