Operator Precedence in Unary Operators
Unary operators operate on a single operand. The precedence of unary operators is higher than arithmetic, relational, logical, and assignment operators.
Below is the implementation of Operator Precedence in Unary Operators:
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int y = 3;
int result = ++x * -y;
cout << "(++x * -y) = " << result << endl;
x = 5; // Reset x to its initial value
result = (++x) * (-y);
cout << "After expression (++x) * (-y) = " << result
<< endl;
x = 5; // Reset x to its initial value
result = -x * -y;
cout << "After expression (-x * -y) = " << result
<< endl;
x = 5; // Reset x to its initial value
result = (-x) * (-y);
cout << "After expression (-x) * (-y) = " << result
<< endl;
return 0;
}
Output
+a: 5 -a: -5 a++: 6 a--: 5 !a: 0
Operator Precedence in Programming
Operator Precedence, also known as operator hierarchy, is a set of rules that controls the order in which operations are performed in an expression without parentheses. It is a fundamental concept in programming languages and is crucial for writing correct and efficient code.
Table of Content
- What is Operator Precedence?
- Operator Precedence in Arithmetic Operators
- Operator Precedence in Relational Operators
- Operator Precedence in Logical Operators
- Operator Precedence in Assignment Operators
- Operator Precedence in Bitwise Operators
- Operator Precedence in Conditional (Ternary) Operator
- Operator Precedence in Unary Operators
- Operator Precedence in Member Access Operators
- Operator Precedence in Type Cast Operators
- Importance of Operator Precedence
Contact Us