Updated July 10, 2023
Introduction to Unary Operators in C++
Unary operators are the operators that operates on single operand to give the specific result. To perform the operations on these operators we need to use only single operand. C++ provides various unary operators like unary plus operator, unary minus operator, increment operator, decrement operator, address of operator, size of operator, logical NOT, dereferencing operator, and bitwise NOT operator. These operators has right-left associativity, syntax is similar to the postfix operators.
Different Operators of Unary in C++
There are nine unary operators available in C++ programming language as mentioned below:
1. Unary Plus
A unary plus operator is denoted by symbol “+” and this operator doesn’t make any changes on the operand value. It always represents the value of the operands.
Syntax:
+
Code:
#include<iostream>
using namespace std;
int main()
{ int y = + 10;
printf("y = %d \n" , y);
return 0;
}
Output:
2. Unary Minus
A unary minus operator is denoted by the symbol “-” and this operator makes changes on the operand value and as a result, it makes the given value negative. Convert a positive value to negative value and negative value to positive value. It always represents the value of the operands.
Syntax:
-
Code:
#include<iostream>
using namespace std;
int main() {
int y;
float x = 2.34;
y = (int) x;
x = -x;
cout << x;
return 0;
}
Output:
3. Increment operator
The increment operator is denoted by symbol “++”. Increment operators always increase the value by 1. Usually two type post-increment and pre-increment operator.
Syntax:
++
Code:
#include <stdio.h>
using namespace std;
int main()
{
int p=10;
int q=20;
printf("The value of p: %d\n",++p);
printf("The value of q: %d\n",q++);
return 0;
}
Output:
4. Decrement operator
Decrement operator is denoted by the symbol “–”.The decrement operator always decreases the value by 1. Usually two type post-decrement and pre-decrement operator.
Syntax:
--
Code:
#include <stdio.h>
using namespace std;
int main()
{
int p=10;
int q=20;
printf("The value of p: %d\n",--p);
printf("The value of q: %d\n",q--);
return 0;
}
Output:
5. Address of the operator
The address of the operator is denoted by the symbol “&” This operator returns the address of any variable. As it usually takes the address of its operand. The operand of the address of the operator can be a function or an Integer value that resides in an object.
Syntax:
&
Code:
#include <stdio.h>
using namespace std;
int main()
{
int x, p=20;
printf("Value of p: %d\n",x);
printf("Address of p: %X\n",&p);
return 0;
}
Output:
6. Size of the operator
The size of the operator is denoted by the symbol “sizeof()”. The size of the operator acts like a function. This operator always returns the variable and object occupied size. This operator also returns the size of any data types. It is also known as a compile-time unary operator.
Syntax:
sizeof()
Code:
#include <stdio.h>
using namespace std;
int main()
{
int p=20;
printf("The size of p : %d\n",sizeof(p));
printf("The size of 20 : %d\n",sizeof(20));
printf("The size of int : %d\n",sizeof(int));
printf("The size of char : %d\n",sizeof(char));
printf("The size of float: %d\n",sizeof(float));
return 0;
}
Output:
7. Dereferencing operator
Dereferencing operator is denoted by the symbol “*”. This operator returns an integer value which is equal to pointer address value. This operator is also known as an indirection operator.
Syntax:
*
Code:
#include <iostream>
using namespace std;
int main() {
int x = 5;
int *yx = &x;
int **yyx = &yx;
cout << "Value of x:\n"
<< "the direct value: " << x << endl
<< "the indirect value: " << *yx << endl
<< "the doubly indirect value: " << **yyx << endl
<< "the address of x: " << yx << endl
<< "the address of x via indirection: " << *yyx << endl;
}
Output:
8. Logical NOT
Logical NOT operator is denoted by the symbol “!”. This operator reverses the meaning of its operand. This operator is also known as a Logical Negation Operator. Implicitly the operand will be converted to a Boolean data type.
Syntax:
!
Code:
#include <iostream>
using namespace std;
int main() {
int x = 0;
if (!x)
cout << "x is zero" << endl;
}
Output:
9. Bitwise NOT/One’s Compliment
Bitwise NOT operator is denoted by symbol “ ~ ”. This operator yields the bitwise one’s complement of the operand. This operator is also known as One’s Complement Operator. It means every 1 will be converted to 0 as a result.
Syntax:
~
Code:
#include <iostream>
using namespace std;
int main () {
unsigned short x = 0xFFFF;
cout << std::hex << x << endl;
x = ~x;
cout << std::hex << x << endl;
}
Output:
Let’s take one more example to represent functionalities of unary operators through single C++ code:
#include <iostream>
using namespace std;
int main()
{
int x = 1;
cout << "x value: " << x << endl;
int y = x++; // post increment
cout << "y value after x++ : " << y << endl;
cout << "x value after x++ : " << x << endl;
x = 5;
cout << "x value: "<< x<<endl;
y = --x;
cout << "y value after --x : " << y << endl;
cout << "x value after --x : " << x << endl;
return 0;
}
Output:
Conclusion
The unary operator is very useful for performing various calculations and type conversion in the smallest possible time. Code efficiency can be increased by using these unary operators because you don’t need to create a function for basic tasks that can be done by operators within a bit amount of time.
Recommended Articles
This has been a guide to the Unary Operators in C++. Here we also discuss the basic concept with top 9 unary operators in c++. You may also have a look at the following articles to learn more–