Unary operator: are operators that act upon a single operand to produce a new value.
Types of unary operators:
- unary minus(-)
- increment(++)
- decrement(- -)
- NOT(!)
- Addressof operstor(&)
- sizeof()
- unary minus
The minus operator changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive.int a = 10; int b = -a; // b = -10
unary minus is different from subtraction operator, as subtraction requires two operands.
-
increment
It is used to increment the value of the variable by 1. The increment can be done in two ways:- prefix increment
In this method, the operator preceeds the operand (e.g., ++a). The value of operand will be altered before it is used.
int a = 1; int b = ++a; // b = 2
- postfix increment
In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used.int a = 1; int b = a++; // b = 1 int c = a; // c = 2
- prefix increment
- decrement
It is used to decrement the value of the variable by 1. The increment can be done in two ways:- prefix decrement
In this method, the operator preceeds the operand (e.g., ++a). The value of operand will be altered before it is used.int a = 1; int b = --a; // b = 0
- posfix decrement
In this method, the operator follows the operand (e.g., a- -). The value of operand will be altered after it is used.int a = 1; int b = a--; // b = 1 int c = a; // c = 0
C++ program for combination of prefix and postfix operations:
// C++ program to demonstrate working of unary increment
// and decrement operators
#include <iostream>
using
namespace
std;
int
main()
{
// Post increment
int
a = 1;
cout <<
"a value: "
<< a << endl;
int
b = a++;
cout <<
"b value after a++ : "
<< b << endl;
cout <<
"a value after a++ : "
<< a << endl;
// Pre increment
a = 1;
cout <<
"a value:"
<< a << endl;
b = ++a;
cout <<
"b value after ++a : "
<< b << endl;
cout <<
"a value after ++a : "
<< a << endl;
// Post decrement
a = 5;
cout <<
"a value before decrement: "
<< a << endl;
b = a--;
cout <<
"b value after a-- : "
<< b << endl;
cout <<
"a value after a-- : "
<< a << endl;
// Pre decrement
a = 5;
cout <<
"a value: "
<< a<<endl;
b = --a;
cout <<
"b value after --a : "
<< b << endl;
cout <<
"a value after --a : "
<< a << endl;
return
0;
}
Output:
a value: 1 b value after a++ : 1 a value after a++ : 2 a value:1 b value after ++a : 2 a value after ++a : 2 a value before decrement: 5 b value after a-- : 5 a value after a-- : 4 a value: 5 b value after --a : 4 a value after --a : 4
The above program shows how the postfix and prefix works.
- prefix decrement
- NOT(!): It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
If x is true, then !x is false If x is false, then !x is true
- Addressof operator(&): It gives an address of a variable. It is used to return the memory address of a variable. These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.
& gives an address on variable n int a; int *ptr; ptr = &a; // address of a is copied to the location ptr.
- sizeof(): This operator returns the size of its operand, in bytes. The sizeof operator always precedes its operand.The operand is an expression, or it may be a cast.
#include <iostream>
using
namespace
std;
int
main()
{
float
n = 0;
cout <<
"size of n: "
<<
sizeof
(n);
return
1;
}
Output:
size of n: 4
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments