Unary Operators in C++

Unary Operators in C++
Techiio-author
Written by Sagar RabidasFebruary 7, 2022
9 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Unary Operators in C++

Unary Operators in C++

Unary operators are the operators that operate on an unmarried operand to present a particular result. To carry out the operations on those operators we want to use the simplest unmarried operand. C++ provides various unary operators like unary plus operator, unary minus operator, increment operator, decrement operator, deal with of operator, size of the operator, logical now not, dereferencing operator, and bitwise no longer operating. These operators have right-left associativity, the syntax is just like the postfix operators.

Different Operators of Unary in C++

There are nine unary operators available in the C++ programming language as mentioned below:

  • Unary Plus:-

A unary plus operator is denoted by the 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;
}
  • 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 a negative value and a negative value to a 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;
}
  • Increment operator:-

The increment operator is denoted by the symbol “++”. Increment operators always increase the value by 1. Usually, two types are post-increment and pre-increment operators.

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;
}
  • Decrement operator:-

Decrement operator is denoted by the symbol “–”.The decrement operator always decreases the value by 1. Usually, two types are post-decrement and pre-decrement operators.

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;
}
  • 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;
}
  • Size of the operator:-

The scale of the operator is denoted using the symbol “sizeof()”. The scale of the operator acts as a characteristic. This operator usually returns the variable and object occupied length. This operator additionally returns the dimensions of any statistics kinds. It's also known as a collect-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;
}
  • Dereferencing operator:-

Dereferencing operator is denoted by the symbol “*”. This operator returns an integer value that is equal to the 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;
}
  • Logical NOT:-

The logical now not operator is denoted by the symbol “!”. This operator reverses the which means of its operand. This operator is also known as a logical negation operator. Implicitly the operand could be converted to a boolean information type.

Syntax:

!

Code:

#include <iostream>
using namespace std;
int main() {
int x = 0;
if (!x)
cout << "x is zero" << endl;
}
  • Bitwise NOT/One’s Compliment:-

Bitwise not operator is denoted via image “ ~ ”. This operator yields the bitwise one’s complement of the operand. This operator is also called one’s supplement operator. It means each 1 can be transformed 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;
}

Let’s take one more example to represent functionalities of unary operators through a 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;
}

C
C++
Unary Operators
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance