# Unary Operators in C++

Unary operators in C++

Unary operator: is operations that function to produce a new value on a single operand.

a) unary minus: A minus operator modifies the argument's symbol. A positive number becomes negative, so it becomes a positive negative number.

```int x = 15;
int y = -x;  // y = -15```

Unary minus is dissimilar from decrement operator since two operands are required for subtraction.

b) Increment: It's being used to raise the variable value by 1. The raise can be done in two different ways

prefix increment

The operator precedes the operand by this method (+ + a). Until it is was using the operand value would be altered.

```int x = 4;
int y = ++x;  // y = 8```

postfix increment

The operator follows the operand in this procedure (a + +). After it has been were using the value operand will be changed.

```int x = 2;
int y = x++;   // y = 2
int z = x;     // z = 4```

c) decrement

It's being used to decrease the variable value by 1. The rise can be affected in 2 directions

prefix decrement

The operator precedes the substring in this technique (– -a). Until it has been was using, the operand value would be altered.

```  int x = 2;
int y = --x;  // y = 1```

posfix decrement

The operator tends to follow the iterator in that method ( a--). Once it's been included, the operand value is modified.

```int x = 1;
int y = x--;   // y = 1
int z = x;     // z = 0```

C++ program for Prefix and Postfix combination operations:

```// C++ program to demonstrate working of unary increment
// and decrement operators
#include <iostream>
using namespace std;
int main()
{
// Post increment
int x = 2;
cout << "x value: " << x << endl;
int y = x++;
cout << "y value after x++ : " << y << endl;
cout << "x value after x++ : " << x << endl;
// Pre increment
x = 2;
cout << "x value:" << x << endl;
y = ++x;
cout << "y value after ++x : " << y << endl;
cout << "x value after ++x : "<< x << endl;
// Post decrement
x = 7;
cout << "x value before decrement: " << x << endl;
y = x--;
cout << "y value after x-- : " << y << endl;
cout << "x value after x-- : " << x << endl;
// Pre decrement
x = 7;
cout << "x value: "<< x<<endl;
y = --x;
cout << "y value after --x : " << y << endl;
cout << "x value after --x : " << x << endl;
return 0;
}```

Output:

d) NOT(!): This is used to change its operand 's logical condition. If a statement is fulfilled, therefore the operator Logical NOT will make it not true.

```If y is true, then !y is false
If y is false, then !y is true```

e) Addressof operator(&): This provides a variable url. It serves to revert back a variable's memory location. Such addresses returned by the operator's address are recognized as reference points because they "point" in memory to the factor.

```& gives an address on variable n
int x;
int *ptr;
ptr = &x;```

f) sizeof(): That operator produces its operand length, in bytes. The operator's length every time invokes its operand. An expression is an operand, or it may be a cast.

```#include <iostream>
using namespace std;
int main()
{
float m = 0;
cout << "Volume of m: " << sizeof(m);
return 1;
}```

Output: