Unary Operators In C++
Last Updated :
12 Nov, 2023
Unary operators in C++ are those operators that work on a single value (operand). They perform operations like changing a value’s sign, incrementing or decrementing it by one, or obtaining its address. Examples include ++ for increment, — for decrement, + for positive values, – for negative values,! for logical negation, and & for retrieving memory addresses. In this article, we will discuss various unary operators available in C++.
Types of Unary Operators in C++
C++ has a total of 8 unary operators which are as follows:
- Increment Operator (++)
- Decrement Operator (–)
- Unary Plus Operator (+)
- Unary Minus Operator (-)
- Logical NOT Operator (!)
- Bitwise NOT Operator (~)
- Addressof Operator (&)
- Indirection Operator (*)
- sizeof Operator
1. Increment Operator (++)
The increment operator is used to increase the value of its operand by 1. It works on the numeric operands and be use used as both prefix and postfix to the operand
Syntax
++ operand
or
operand ++
Example
C++
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout << "Initial value of x: " << x << endl;
x++;
cout << "After x++, x is now: " << x << endl;
int a = 10;
int b, c;
b = ++a;
c = a++;
cout << "a: " << a << ", b: " << b << ", c: " << c
<< endl;
return 0;
}
|
Output
Initial value of x: 5
After x++, x is now: 6
a: 12, b: 11, c: 11
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 --
or
-- operand
Example
C++
#include <iostream>
using namespace std;
int main()
{
int var = 10;
cout << "Value before decrement: " << var << endl;
var--;
cout << "Value after decrement: " << var;
return 0;
}
|
Output
Value before decrement: 10
Value after decrement: 9
To know more about increment and decrement operators, refer to the article – C++ Increment and Decrement Operators
3. Unary Plus Operator (+)
The symbol (+) represents the unary plus operator in C++, which explicitly specifies the positive value of its operand. It doesn’t change the sign of a if it’s already positive, but it can be used for clarity in expressions.
– is the unary minus operator, which changes the sign of its operand to negative.
Explanation:
- The unary minus (-) changes the sign of the value. If a is positive, -a makes it negative.
- The unary plus (+) is used to explicitly specify a positive value.
Syntax
+ operand
Example
C++
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int c = +a;
cout << "Original value of a: " << a << endl;
cout << "After using unary plus (+a): " << c << endl;
return 0;
}
|
Output
Original value of a: 10
After using unary plus (+a): 10
4. Unary Minus (-) Operator
The symbol (+) represents the unary plus operator in C++, 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
Example
C++
#include <iostream>
using namespace std;
int main()
{
int var = 20;
cout << "Initial Value: " << var << endl;
cout << "Value with (-): " << -var;
return 0;
}
|
Output
Initial Value: 20
Value with (-): -20
5. Logical NOT Operator (!)
The logical NOT operator (!) is used for negating the value of a boolean expression. It returns true if the operand is false, and false if the operand is true.
Note: Zero is considered false and any non zero value is considered true in C++.
Syntax
! operand
Example
C++
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
bool isTrue = true ;
bool isFalse = !isTrue;
cout << boolalpha;
cout << "Value of isTrue: " << isTrue << endl;
cout << "Value of isFalse (!isTrue): " << isFalse
<< endl;
return 0;
}
|
Output
Value of isTrue: true
Value of isFalse (!isTrue): false
6. Bitwise NOT Operator (~)
The bitwise NOT operator ~ in C++ performs a bitwise negation operation on integral data types, such as integers. It inverts each bit of the operand, changing every 0 bit to 1 and every 1 bit to 0′.
Syntax
~ operand
Example
C++
#include <iostream>
using namespace std;
int main()
{
unsigned int a = 5;
unsigned int b = ~a;
cout << "Original value of a: " << a
<< " (Binary: 0000 0101)" << endl;
cout << "Value of b after ~a: " << b
<< " (Binary: 1111 1010)" << endl;
return 0;
}
|
Output
Original value of a: 5 (Binary: 0000 0101)
Value of b after ~a: 4294967290 (Binary: 1111 1010)
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
Example
C++
#include <iostream>
using namespace std;
int main()
{
int number = 42;
int * ptr = &number;
cout << "Value of number: " << number << endl;
cout << "Address of number: " << &number << endl;
return 0;
}
|
Output
Value of number: 42
Address of number: 0x7fff5785a304
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 Sddress.
Syntax
* operand
Example
C++
#include <iostream>
using namespace std;
int main()
{
int var = 10;
int * ptr = &var;
cout << "var = " << var << endl;
cout << "*ptr = " << *ptr;
return 0;
}
|
Output
var = 10
*ptr = 10
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 (operand)
Example
C++
#include <iostream>
using namespace std;
int main()
{
int arr[10];
cout << "Integer Size: " << sizeof ( int ) << endl;
cout << "Size of Integer Array with Elements: "
<< sizeof (arr);
return 0;
}
|
Output
Integer Size: 4
Size of Integer Array with Elements: 40
Precedence and Associativity of Unary Operators in C++
Precedence of Unary Operators
Unary operators generally have a higher precedence than most binary operators. They have a fixed order of execution before most binary operations.
Associativity of Unary Operators
Unary operators are right-associative, meaning that if multiple unary operators are applied to the same operand, they are evaluated from right to left.
Example
- int a = 5;: Initializes variable ‘a’ with the value 5.
- int b = -++a;: In this expression:
- ++a is a prefix increment, which increments the value of ‘a’ by 1.
- – is the unary minus operator that changes the sign of the value obtained from ++a.
- Then display the initial value of ‘a’ and the value of ‘b’ after applying the unary operators.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = -++a;
cout << "Initial value of a: " << a << endl;
cout << "Value of b (-++a): " << b << endl;
return 0;
}
|
Output
Initial value of a: 6
Value of b (-++a): -6
Share your thoughts in the comments
Please Login to comment...