You are here:

C++/int x = a+++b;



I've tried the following code snippet, why was the value of x 12 ? but not 13?
#include <iostream>
using namespace std;

int main() {
int a = 5;
int b = 7;
int x = a+++b;
return 0;

Well although a++ is evaluated first because post-increment has a higher precedence than add the fact is that you are using _post_ increment - that is the result of a++ is the original value of a, then a is incremented, thus the value of a++ passed to + is 5, then a becomes 6. Think of it like this:

   int tmp = a++;     // tmp initialised to 5, a incremented to 6
   int x = tmp + c;   // x = 5 + 7 = 12

Note that if you are implementing your own post-increment operator then it is usually implemented in terms or pre-increment, thus:

   class T
       T & operator++() { ... }   // pre-increment

       T   operator++(int)        // post-increment
         T tmp(*this); // take copy of current state of this object
         ++*this;      // use pre-increment operator to increment this object
         return tmp;   // return saved initial state object by _value_

Notice how much extra work post-increment requires in addition to that done by pre-increment. A copy to start with (tmp) and a copy to end with (tmp returned by value). Whereas pre-increment operators return by reference. This is why C++ people prefer to use pre-increment in cases where pre- or post- increment can be used, using ++i as opposed to i++ in for-loops for example. This argument also applies to pre- and post- decrement of course.

Of course for the built in types such as int there is probably no penalty for using i++ over ++i - even if the processor type would course such a problem (due to lack of support or some such) hopefully the compiler would recognise that in cases such as i++ ++i can be used just as well. However for user defined types for which there are overloaded pre- and post- increment operators the compiler has to 'call' the overloaded operator function for the operator in question. So the compiler will add a call to the overloaded post-increment operator to the generated code if i++ is used with all its overhead as compared with pre-increment. I say call above but of course the call could be inlined if the function is indicated as such and the compiler agrees (inline is always no more than a hint to the compiler which it is free to ignore debug builds typically do no inlining so that debugger breakpoints can be set on/in inline functions, and such functions can be single stepped through for example).  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.