+ 10

Increment and decrement confuses me ...help plz

#include <iostream> using namespace std; int main() { int x=5; int p=5; int y=x+--x; int q=p+++p; cout<<y<<" "<<q; return 0; } output: 8 11 expected : 8 12 or 9 11

6th Mar 2017, 4:50 AM
manoj katari
manoj katari - avatar
4 ответов
+ 17
int q = p+++p is ambiguous and is interpreted differently by different compilers.
6th Mar 2017, 4:53 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
in general words: Increment: you add something to your variable (e.g: p++) decrement: you subtract something of it (e.g: p--)
6th Mar 2017, 10:11 AM
LayB
LayB - avatar
+ 6
'+' operator and prefix/postfix operator have the same precedence. Which means that none of them come first or come last when processing them. Its like 3+4+5. You can add 4 and 5 first. Or you can add 3 and 4 first. In this case, we call this "Undefined Behaviour". Where the compiler may give different possible results.
6th Mar 2017, 8:52 AM
Wen Qin
Wen Qin - avatar
25th Jun 2017, 10:10 AM
Siddharth Saraf