Unary operators are operators that perform an operation on a single operand. They play an important role in modifying values, evaluating conditions, and managing memory in C++.
- Unary operators help simplify many common programming tasks by operating directly on a single value.
- They are widely used in arithmetic, logical, bitwise, and pointer-based operations.
Types of Unary Operators in C++
The following table summarizes the commonly used unary operators in C++:
| Operator | Name | Purpose |
|---|---|---|
| ++ | Increment | Increases value by 1 |
| -- | Decrement | Decreases value by 1 |
| + | Unary Plus | Returns the operand value unchanged |
| - | Unary Minus | Changes the sign of the operand |
| ! | Logical NOT | Reverses the logical value |
| ~ | Bitwise NOT | Inverts all bits |
| & | Address-of | Returns memory address |
| * | Dereference | Accesses value stored at an address |
| sizeof | Sizeof | Returns size of data type or variable |
1. Increment Operator (++)
The increment operator increases the value of its operand by 1. It can be used in two forms:
- Prefix Increment (++operand) - increments the value first and then uses it.
- Postfix Increment (operand++) - uses the current value first and then increments it.
++operand; // Pre-Increment
operand++; // Post-Increment
#include <iostream>
using namespace std;
int main() {
// Two integer variables
int a = 10;
int b, c;
// Prefix increment: first, increment a, then
// assign to b
b = ++a;
// Postfix increment: first, assign a to c,
// then increment a
c = a++;
cout << "a: " << a << ", b: " << b << ", c: " << c;
return 0;
}
Output
a: 12, b: 11, c: 11
Explanation:
- After b = ++a, the value of a becomes 11 and that value is assigned to b.
- After c = a++, the current value of a (11) is assigned to c, and then a is incremented to 12.
2. Decrement Operator (--)
The decrement operator is used to decrement the value by 1. Just like the increment operator, it can be used as pre-decrement and post-decrement. It works on numeric values.
Syntax
--operand; // Pre-Decrement
operand--; // Post-Decrement
#include <iostream>
using namespace std;
int main() {
// Two integer variables
int a = 10;
int b, c;
// Prefix decrement: first, decrement a, then
// assign to b
b = --a;
// Postfix decrement: first, assign a to c,
// then decrement a
c = a--;
cout << "a: " << a << ", b: " << b << ", c: " << c;
return 0;
}
Output
a: 8, b: 9, c: 9
Explanation: The prefix decrement decreases the value before it is used, while the postfix decrement uses the current value first and then decreases it.
3. Unary Plus Operator (+)
The unary plus operator returns the value of its operand without changing it. Although it rarely changes program behavior, it can participate in type promotions and improve code readability.
Syntax
+ operand;
#include <iostream>
using namespace std;
int main() {
int a = 10;
// Unary Plus: Explicitly specifies 'a' as
// positive
int c = +a;
cout << "a: " << a << endl;
cout << "+a: " << c;
return 0;
}
Output
a: 10 +a: 10
4. Unary Minus (-) Operator
The symbol (-) represents the unary minus operator, which changes the sign of its operand to negative. If the operand is negative, this operator will make it positive and vice versa.
Syntax
- operand;
#include <iostream>
using namespace std;
int main() {
int a = 10;
// Unary minus: Changes a to -a
int c = -a;
cout << "a: " << a << endl;
cout << "-a: " << c;
return 0;
}
Output
a: 10 -a: -10
5. Logical NOT Operator (!)
The logical NOT operator reverses the logical value of its operand. It returns true if the operand evaluates to false, and false if the operand evaluates to true.
Syntax
! operand;
#include <bits/stdc++.h>
using namespace std;
int main() {
bool isTrue = true;
// Logical NOT negates the value of 'isTrue'
bool isFalse = !isTrue;
// Print string correspoinding to true and false
cout << boolalpha;
cout << "Value of isTrue: " << isTrue << endl;
cout << "Value of isFalse (!isTrue): " << isFalse;
return 0;
}
Output
Value of isTrue: true Value of isFalse (!isTrue): false
6. Bitwise NOT Operator (~)
The bitwise NOT operator inverts every bit of an integral operand. Every 0 becomes 1 and every 1 becomes 0.
Syntax
~ operand;
#include <iostream>
using namespace std;
int main() {
// Binary: 0000 0101
unsigned int a = 5;
// Bitwise NOT: Inverts each bit of 'a'
unsigned int b = ~a;
cout << "a: " << a << " (Binary: 0000 0101)\n";
cout << "~a: " << b << " (Binary: 1111 1010)";
return 0;
}
Output
a: 5 (Binary: 0000 0101) ~a: 4294967290 (Binary: 1111 1010)
Explanation: The binary representation of 5 is inverted bit by bit, producing a large unsigned value.
7. Addressof Operator (&)
The addressof operator (&) retrieves the memory address of a variable. It returns the memory location where the variable is stored in the computer's memory.
Syntax
& operand;
#include <iostream>
using namespace std;
int main() {
int n = 42;
// Print address of n
cout << &n;
return 0;
}
Output
0x7fff5a12b02c
The exact memory address will vary each time the program executes.
8. Dereference Operator (*)
The dereference operator (*) is used to access the value at a specific memory address stored in a pointer. It points to the value stored at that address.
Syntax
* operand;
#include <iostream>
using namespace std;
int main() {
int var = 10;
// Pointer to var
int* ptr = &var;
// Accessing value pointed by ptr i.e. var
cout << *ptr;
return 0;
}
Output
var = 10 *ptr = 10
Explanation: The pointer ptr stores the address of var. Dereferencing it using *ptr retrieves the value stored at that address.
9. sizeof Operator
The sizeof operator is a special unary operator in C++ that returns the size of its operand it bytes. Its operand can be any data type or a variable.
Syntax
sizeof(type)
sizeof(variable)
#include <iostream>
using namespace std;
int main() {
// Finding size of integer
cout << "Integer Size: " << sizeof(int) << " bytes";
return 0;
}