C++ has many different operators which are used to perform mathematical and logical operations on the numerical values. One such widely used operator is the Unary Operators. In mathematics as well as in the programming language, Unary Operator is an operation that is performed on only one operand. Unary Operators are in contrast to the Binary operators, which use two operands to calculate the result. Let us learn more about the Unary Operators, their different types, and examples in this article.
Definition
- Unary operators are operators which are used to calculate the result on only one operand.
- Unary operators are used on a single operand in order to calculate the new value of that variable.
Unary Operators
Unary operators can be used either in the prefix position or the postfix position of the operand. To perform operations using Unary Operators, we need to use one single operand. There are various types of Unary operators and all these types are of equal precedence, having right-to-left associativity.
Syntax
For a Prefix Unary Operator:
operator expression ;
For a Postfix Unary Operator:
expression operator ;
where,
variable can also be used in place of expressions.
Note: In C++, Both the Prefix and Postfix Unary operator positions have the same functional values and produce the same results if they are not used in any expression format. Look at the below example for understanding the concept.
Example
int main()
{
int x = 10 ; int y = 20 ;
++x ;
y++ ;
cout << x << endl << y ;
}
Output
11
21
Note: In C++, If the Increment(++) or Decrement(–) Operator is used in an expression, then in the Prefix position, Increment/Decrement will be done before the expression. In the Postfix position, Increment/Decrement will be done after the complete expression has been evaluated.
Browse all the Topics Under Operator and Expressions: Operators
- Arithmetic Operators
- Assignment Operator
- C++ Shorthands
- Increment and Decrement Operators
- Relation Operator
- Logical Operators
Types of Unary Operators
Operator | Operator Name | Description |
+ | Unary Plus | Does not change any value |
– | Unary Minus | Changes the sign of the Value |
++ | Increment | Prefix/Postfix adds 1 to its operand |
— | Decrement | Prefix/Postfix subtracts 1 to its operand |
& | Address-of | Returns the Address of the operand |
* | Indirection Operator | Points to the value stored in the memory location of the operand |
! | Logical Negation | Reverses logical state of the operand |
sizeof() | Sizeof | Returns size of the operand |
~ | One’s Compliment | Bitwise One’s Compliment |
-
Unary Plus (+)
This Unary Operator is denoted by the ‘+’ sign and it does not make any difference to the value of the operand. It is just used to denote that the operand is of a positive value.
Example
int main()
{
int y = + 10 ;
cout<<" Value of y = "<
Output
Value of y = 10
-
Unary Minus (-)
The ‘-’ sign is used in this Unary Operator type and its main function is that it changes the sign value of the operand. It has the function of converting a positive value to a negative value and vice versa.
Example –
int main()
{
int x = 10 ;
x = -x ;
cout<<" Value of x = "<
Output
Value of x = -10
-
Unary Increment (++)
In this type, the Unary Operator is denoted by the ‘++’ sign. It increases the value by 1 to the operand and then stores the value to the variable. It works for both Prefix and Postfix positions. Remember – The Prefix and Postfix work differently and provide different computational results.
Example
int main()
{
int x = 10 ;
int y = 20 ;
cout<<" Value of x = "<< x++ ;
cout<<" Value of y = "<< ++y ;
return 0 ;
}
Output
Value of x = 10
Value of y = 21
-
Unary Decrement (–)
This type of Unary Operator is denoted by the ‘–’ sign. It decreases the value by 1 to the operand and then stores the value to the variable. Just like the Unary Increment Operator it also works for both Prefix and Postfix positions. Remember – The Prefix and Postfix work differently and provide different computational results.
Example
int main()
{
int a = 10 ;
int b = 20 ;
cout<<" Value of a = "<< a-- ;
cout<<" Value of b = "<< --b ;
return 0 ;
}
Output –
Value of a = 10
Value of b = 19
-
sizeof()
Sizeof Unary Operator is used to return the size of the operand, in bytes. For example, in C++, the int data type is of the size 4 bytes. This value will be returned by the sizeof operator. The sizeof() operator is used as a function in the program.
Example
int main()
{
int a = 10 ;
double b = 20.34 ;
cout<<" Size of a = "<< sizeof(a) ;
cout<<" Size of b = "<< sizeof(b) ;
return 0 ;
}
Output
Size of a = 4
Size of b = 8
-
Addressof Operator (&)
In this type, the operator returns the address of the variable stored in the memory location. It returns the address value of the memory location where the variable is stored.
Example
int z;
int *ptr;
ptr = &z; // address of a is copied to the location ptr
-
Indirection Operator (*)
This type of operator returns the value stored in the memory location by pointing it to the memory location of the variable.
Example –
int main()
{
int count = 24 ;
int *P = &count ;
cout<< “ Value of *P = ”<< *P ;
}
Output
Value of *P = 24
FAQs on Unary Operator
Q1. What is a Unary Operator?
- Operators used to perform calculations on double operands
- Operators used to perform calculations on a single operand
- Operators used to perform calculations on three operands
- Operators used to perform calculations on multiple operands
Answer. Option B
Q2. Which of the following is a Unary Operator?
- ++
- sizeof()
- %
- Both A and B
Answer. Option D
Q3. What is the output of the following code?
int main()
{
int q = - -30 ;
cout<<"q = "<< q ;
}
- 30
- -30
- 0
- None of the Above
Answer. Option A
Q4. Which of the following statements are true about the ‘++’ operator?
- It is used as prefix and postfix
- It is used only on a single operand
- It cannot be applied to an expression
- All of the Above
Answer. Option D
Leave a Reply