You are here:

C++/increment decrement opertors

Advertisement


Question
how do increment and decrement operators work in an expression? is it that first all increment then all decrement and then other operations are executed.
please clear this with some examples. is the execution same in both C and C++.
please tell how these are executed if x=1 initially.
y=++x + ++x + 5;
y=x++ + x++ + 5;
y=x++ + ++x + x++ + 10;


Answer
When you have increment (pre or post) or decrement of the same variable in an expression, by definition of the C/C++ ISO standard, the behavior (result) is undefined.

All of your examples are undefined - ++ and -- shouldn't be used like this, and because of the definition, there is no right answer.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.