Click here to Skip to main content
15,891,597 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerMessage Closed Pin
7-Jan-14 0:28
Suk@nta7-Jan-14 0:28 
GeneralRe: Increment and Decrement Operators Pin
OriginalGriff8-Jan-14 1:59
mveOriginalGriff8-Jan-14 1:59 
GeneralRe: Increment and Decrement Operators Pin
Suk@nta9-Jan-14 18:17
Suk@nta9-Jan-14 18:17 
GeneralRe: Increment and Decrement Operators Pin
OriginalGriff9-Jan-14 22:10
mveOriginalGriff9-Jan-14 22:10 
GeneralRe: Increment and Decrement Operators Pin
Suk@nta10-Jan-14 3:52
Suk@nta10-Jan-14 3:52 
AnswerRe: Increment and Decrement Operators Pin
Marco Bertschi7-Jan-14 1:31
protectorMarco Bertschi7-Jan-14 1:31 
AnswerRe: Increment and Decrement Operators Pin
tgsb7-Jan-14 20:38
tgsb7-Jan-14 20:38 
GeneralRe: Increment and Decrement Operators Pin
OriginalGriff8-Jan-14 1:56
mveOriginalGriff8-Jan-14 1:56 
Pre- and post- fix increment and decrement operations are pretty easy in theory, it's only when people get creative that you get problems in practice.
Basically, a prefix (++i or --i) says to increase or decrease the value before you use the variable, so the variable has the new value immediately:
C++
i = 10;
x = ++i + 5;
Can be read as:
C++
i = 10;
i = i + 1;
x = i + 5;
and similarly for the -- version:
C++
i = 10;
x = --i + 5;

Can be read as:
C++
i = 10;
i = i - 1;
x = i + 5;

The postfix version (i++ or i--) does the same thing, but after the variable has been used:
C++
i = 10;
x = i++ + 5;
Can be read as:
C++
i = 10;
x = i + 5;
i = i + 1;
And similarly
C++
i = 10;
x = i-- + 5;

Can be read as:
C++
i = 10;
x = i + 5;
i = i - 1;


The trouble comes when you start mixing operations on the same line (as Richard said):
C++
i = 10;
x = ++i + i++;
The problem is that the language specification does not define exactly when pre- and post- fix operations should occur! Which means that it's implementation specific exactly what you get as a result: The value of i should always be the same: 12 but the value of x can be different depending on the compiler (and to an extent on the target processor - ARM for example has built in pre- and post- fix increment and decrement to it's "machine code" LOAD operations, so it would be quite likely that an efficient compiler would use them directly)

Should it be executed as:
C++
i = 10;
i = i + 1;
x = i + i;
i = i + 1;
Which gives the result 22
or as
C++
i = 10;
i1 = i;
i = i + 1;
x = i1 + i;
i = i + 1;
Which gives 21
Or as
C++
i = 10;
i1 = i;
i = i + 1;
x = i + i1;
i = i + 1;
which also gives 21 by a different route
And bear in mind that the compiler does not have to evaluate the two operands of "+" in left to right order, so it could even give some very strange and unexpected results! Like 23...
So avoid combining them: use them for "simple expressions" such as incrementing an array index each time round a loop, but don't get fancy, or your code may well fail in interesting ways... Laugh | :laugh:
Never underestimate the power of stupid things in large numbers
--- Serious Sam

GeneralRe: Increment and Decrement Operators Pin
tgsb8-Jan-14 19:43
tgsb8-Jan-14 19:43 
GeneralRe: Increment and Decrement Operators Pin
OriginalGriff8-Jan-14 19:44
mveOriginalGriff8-Jan-14 19:44 
QuestionCTreeCtrl::GetNextSiblingItem never null Pin
dliviu6-Jan-14 1:15
dliviu6-Jan-14 1:15 
AnswerRe: CTreeCtrl::GetNextSiblingItem never null Pin
_Flaviu7-Jan-14 20:24
_Flaviu7-Jan-14 20:24 
GeneralRe: CTreeCtrl::GetNextSiblingItem never null Pin
dliviu8-Jan-14 0:55
dliviu8-Jan-14 0:55 
GeneralRe: CTreeCtrl::GetNextSiblingItem never null Pin
_Flaviu8-Jan-14 1:27
_Flaviu8-Jan-14 1:27 
GeneralRe: CTreeCtrl::GetNextSiblingItem never null Pin
dliviu8-Jan-14 1:36
dliviu8-Jan-14 1:36 
QuestionShort time format Pin
_Flaviu6-Jan-14 0:42
_Flaviu6-Jan-14 0:42 
SuggestionRe: Short time format Pin
Richard MacCutchan6-Jan-14 0:56
mveRichard MacCutchan6-Jan-14 0:56 
GeneralRe: Short time format Pin
_Flaviu6-Jan-14 1:06
_Flaviu6-Jan-14 1:06 
QuestionRe: Short time format Pin
_Flaviu6-Jan-14 1:40
_Flaviu6-Jan-14 1:40 
GeneralRe: Short time format Pin
Richard MacCutchan6-Jan-14 2:20
mveRichard MacCutchan6-Jan-14 2:20 
GeneralRe: Short time format Pin
_Flaviu6-Jan-14 2:24
_Flaviu6-Jan-14 2:24 
AnswerRe: Short time format Pin
David Crow6-Jan-14 5:03
David Crow6-Jan-14 5:03 
GeneralRe: Short time format Pin
_Flaviu6-Jan-14 20:44
_Flaviu6-Jan-14 20:44 
AnswerRe: Short time format Pin
Peter Leow6-Jan-14 1:06
professionalPeter Leow6-Jan-14 1:06 
Question[build c++ by command line on windows OS][visual studio 2008] Pin
Thong LeTrung5-Jan-14 21:32
Thong LeTrung5-Jan-14 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.