0

Please tell me what this line will print in c++

int a = 5;
cout<<++a <<" " <<++a <<" " <<++a <<endl;

in the book "Schaum's programming with c++" it is given that it will proceed from right to left and output will be 8 7 6

but I am getting output as 8 8 8 Please explain, is there any modification in the C++ language after the book was written?

  • 1
    A duplicate of a duplicate of a duplicate. Duplicateception. –  Aug 10 '13 at 18:44
  • 4
    Are you quoting directly from that book? Did the book actually have that code snippet and describe its behavior? Or are you possibly extrapolating from something that the author said? If it did, then throw the book away. There have been modifications to the language since that book was written, but not on this issue. – Benjamin Lindley Aug 10 '13 at 18:46
  • Please get a better (and more current) book: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Blastfurnace Aug 10 '13 at 18:46
  • Can see this answer http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc – Parag Paul Aug 10 '13 at 18:58

1 Answers1

3

It's undefined behavior; you could get any results, your program could crash, or send an insulting letter to your boss. Any book which has this as an example, and specifies some specific output for it, should be thrown in the trash; the author doesn't know C++;

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • No actually the program is showing the same output i.e. 8 8 8 every time I run the program. Also the Book that I mentioned is a very good book and suggested by a good number of persons. The author is "John Hubbard". – user2671040 Aug 10 '13 at 19:11
  • The program has undefined behavior. It can output anything. The fact that with one particular compiler, and one particular build, it has one particular output, means nothing. And any book which presents code such as you show, and suggests that it has some defined behavior, is _not_ a good book, and its author doesn't know the first thing about C++. – James Kanze Aug 12 '13 at 08:16