Unary operators are utilized with a single operand and can perform operations on integers and various data types, similar to binary operators, except for the bool data type.
Unary operators are of two types:
postfix (x++
and x--
)
prefix (++x
and --x
).
The table below provides insight into the functioning of increment and decrement unary operators:
Symbol | Meaning | Example | Example |
++ | Increment (increase the value of variable by 1) | y = ++x; The pre-increment operator first increments x by 1 and then stores the incremented value in y. | y = x++; The post-increment operator first stores the value of x in y and then increments x by 1. |
-- | Decrement (decrease the value of variable by 1) | y = --x; The pre-decrement operator first decrements x by 1 and then stores the decremented value in y. | y = x--; The post-decrement operator first stores the value of x in y and then decrements x by 1. |
a+++++b
is illegal?The reason why the expression a+++++b
is considered invalid is because the combination of post-increment and pre-increment operators results in undefined behavior, which could lead to unexpected outcomes.
The expression a+++++b
works as follows:
The function of expression a++
:
This function will return rvalue that cannot be altered.
// assume a = 10
const int operator++(int &a, int d)
{
temp = a; // temp = 10
a = a + 1; // a = 11
return temp; // return 10
}
The function of expression ++b
:
This function will return an lvalue that can be modified.
// assume b = 5
int & operator++(int &b)
{
b = b + 1; // b = 6
return b; // return 6
}
The compiler first runs the expression a++
, which results in a constant rvalue. The subsequent ++
operation causes a compile error indicating that an lvalue is required, which is not currently available. Thus, the expression will never be executed.
Let’s execute the code below and see the unexpected output.
#include <iostream>using namespace std;int main() {int a=10, b=5;cout<<a+++++b<<endl;return 0;}
a+++++b
To prevent such issues, it is advisable to write expressions in a clear and unambiguous manner, with well-defined behavior. In this particular instance, rewriting the expression as a + (a++) + b
provides a clear meaning to the compiler and ensures the expected results.
a++-++b
is legal?The expression a++-++b
is considered legal and works as follows:
a++
increment the value a
by 1
.++b
increment the value b
by 1
.a++ - ++b
calculates the subtraction between the original value of a
and the new value of b
.a++
, which returns the original value of a
, then evaluates the value of ++b
, which returns the incremented value of b
, and finally performs the subtraction operation on these two values.Let’s run the code below and observe the results:
#include <iostream>using namespace std;int main() {int a=10, b=5;cout<<a++-++b<<endl;return 0;}
a++-++b
a++
returns the rvalue
which is 10
.// assume a = 10
const int operator++(int &a, int d)
{
temp = a; // temp = 10
a = a + 1; // a = 11
return temp; // return 10
}
++b
returns the incremented value of b
which is 6
.// assume b = 5
int & operator++(int &b)
{
b = b + 1; // b = 6
return b; // return 6
}
a++
returns the value 10
, and the pre-increment expression ++b
returns the value 6
. Subtracting these two values results in 4
Free Resources