Unary operators in C++ are operators that work with only one operand (single value or variable). They are commonly used for incrementing, decrementing, logical operations, and memory-related operations.
What are Unary Operators?
Unary operators perform operations on a single operand.
Example:
++a
Here:
ais the operand++is the unary operator
Why Use Unary Operators?
Unary operators are useful because they:
- Simplify code
- Perform quick operations
- Reduce extra statements
- Improve code readability
Types of Unary Operators in C++
Common unary operators include:
- Increment Operator (
++) - Decrement Operator (
--) - Unary Minus (
-) - Logical NOT (
!) - Address Operator (
&) - Dereference Operator (
*) - Sizeof Operator (
sizeof)
Increment Operator (++)
The increment operator increases a variable value by 1.
Example of Increment Operator
#include <iostream>
using namespace std;
int main() {
int a = 5;
++a;
cout << a;
return 0;
}
Output
6
Types of Increment
Pre-Increment
Value increases before use.
++a;
Post-Increment
Value increases after use.
a++;
Decrement Operator (–)
The decrement operator decreases a variable value by 1.
Example of Decrement Operator
#include <iostream>
using namespace std;
int main() {
int a = 10;
--a;
cout << a;
return 0;
}
Output
9
Unary Minus Operator (-)
Changes positive value to negative.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
cout << -a;
return 0;
}
Output
-5
Logical NOT Operator (!)
Reverses the logical value.
Example
#include <iostream>
using namespace std;
int main() {
bool value = true;
cout << !value;
return 0;
}
Output
0
Address Operator (&)
Returns the memory address of a variable.
Example
#include <iostream>
using namespace std;
int main() {
int a = 10;
cout << &a;
return 0;
}
Dereference Operator (*)
Accesses the value stored at a memory address.
Example
#include <iostream>
using namespace std;
int main() {
int a = 10;
int *ptr = &a;
cout << *ptr;
return 0;
}
Output
10
Sizeof Operator
Returns the size of a data type or variable in bytes.
Example
#include <iostream>
using namespace std;
int main() {
int a;
cout << sizeof(a);
return 0;
}
Output
4
Difference Between Unary and Binary Operators
| Unary Operators | Binary Operators |
|---|---|
| Work on one operand | Work on two operands |
Example: ++a | Example: a + b |
Real-Life Example
Think of a light switch:
- ON to OFF
- OFF to ON
Only one item changes state, similar to unary operations.
Why Unary Operators are Important
Unary operators are important because they:
- Simplify operations
- Improve programming efficiency
- Support pointer handling
- Are widely used in loops and conditions
Applications of Unary Operators
Unary operators are used in:
- Loop control
- Pointer operations
- Memory management
- Logical conditions
- Mathematical calculations
Conclusion
Unary operators in C++ work with a single operand and perform operations like incrementing, decrementing, logical reversal, and memory access. They are essential for efficient programming and are widely used in modern C++ applications.