Operator and Expressions: Operators

Unary Operators

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

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

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

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
  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

FAQs on Unary Operator

Q1. What is a Unary Operator?

  1. Operators used to perform calculations on double operands
  2. Operators used to perform calculations on a single operand
  3. Operators used to perform calculations on three operands
  4. Operators used to perform calculations on multiple operands

Answer. Option B

Q2. Which of the following is a Unary Operator?

  1. ++
  2. sizeof()
  3. %
  4. Both A and B

Answer. Option D

Q3. What is the output of the following code?

                    

int main()

{

int q = - -30 ;

 cout<<"q = "<< q ;

}

  1. 30
  2. -30
  3. 0
  4. None of the Above

Answer. Option A

Q4. Which of the following statements are true about the ‘++’ operator?

  1. It is used as prefix and postfix
  2. It is used only on a single operand
  3. It cannot be applied to an expression
  4. All of the Above

Answer. Option D

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.