Decrementation / Incrementation is a basic operation but it's precendence on - --
and + ++
confused me. I'll use decrementation for illustration:
I have a set here of different styles of operating between a
and b
: See it working here
#include <iostream>
using namespace std;
int a=10, b=7;
int main() {
// - and -- opearator // Results: Details:
a = 10, b = 7; cout << a---b << endl; // 3 a post-decrement
a = 10, b = 7; cout << a ---b << endl; // 3 a post-decrement
a = 10, b = 7; cout << a- --b << endl; // 4 b pre-decrement
a = 10, b = 7; cout << a-- -b << endl; // 3 a post-decrement
a = 10, b = 7; cout << a--- b << endl; // 3 a post-decrement
return 0;
}
I understand that the 4
output came from the decremented b
which is 7
that turned to 6
and is subtracted from a
which is 10
.
Also, because of the other four statements, I thought the compiler treats all of them as ---
but behold, here comes the confusion of - --
results. See it working here