5 Replies - 2322 Views - Last Post: 02 January 2009 - 07:42 AM Rate Topic: -----

#1 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 dan_ram  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: prefix increment operator

Posted 02 January 2009 - 07:42 AM

quincy 2005
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1