cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
x++ and ++x great confusion
x++ and ++x great confusion
Sep 7, 2015 at 6:16am UTC
koopey
(160)
HI guys. Can do simple statements involving x++ and ++x in multiple lines, but this expression in single cout statement is giving very weird result.
1
2
int
i=5; cout<<i++<<-i<<++i<<-i<<i;
///output is 6-67-57
more than others, that '5' is very weird. could anyone explain what goes on in step by step basis?
Sep 7, 2015 at 6:24am UTC
Peter87
(11234)
You can't modify and read the same variable inside the same expression like that. The C++ standard just says this is
undefined
, which means anything could happen.
Last edited on
Sep 7, 2015 at 6:24am UTC
Sep 7, 2015 at 6:27am UTC
coder777
(8444)
The order of evaluation is undefined:
http://en.cppreference.com/w/cpp/language/eval_order
So
i++
/
++i
/ and what not may happen before any of the function calls in no specified order.
Sep 8, 2015 at 2:26am UTC
dhayden
(5798)
It's worse than that. You can't have more than one side effect changing a variable in one expression. The results are undefined.
Topic archived. No new replies allowed.