C Language



Admission Enquiry Form

  

Unary Operators in C



What is Unary Operator in C Programming?

Unary operator is that operator which works on one or single operand.

Types of Unary Operators

  1. Increment Operators (++)
  2. Decrement Operators (--)

Further, Increment operators are of two types:

  1. Pre-increment Operator (++num).
  2. Post-increment Operator (num++).

and, Decrement operators are of two types:

  1. Pre-decrement Operator (--num).
  2. Post-decrement Operator (num--).

1. So, in the above image ++ is increment operator and -- is decrement operator. Here num is a variable or an operand.

2. ++ adds 1 to the operand whereas -- subtracts 1 from the operand.

3. num++ increases the value of a variable 'num' by 1 and num-- decreases the value of num by 1.

4. Similarly, ++a increases the value of a variable 'num' by 1 and num-- decreases the value of num by 1.




Example of pre increment operator:


Output

num1=11
num2=11

The above example is of pre-increment operator.In that the value of num1 will increase first by 1 and then assigned to variable num2.So, the value of num1 will be 11 and num2 will also be 11.



Example of post increment operator:


Output

num1=11
num2=10

The above example is of post-increment operator.In that the value of num1 will be assigned first to num2 after that it will increase the value of num1 by 1.So, the value of num1 will be 11 and num2 will also be 10.



Example of pre decrement operator:


Output

num1=9
num2=9

The above example is of pre-decrement operator.In that the value of num1 will decrease first by 1 and then assigned to variable num2.So, the value of num1 will be 9 and num2 will also be 9.



Example of post decrement operator:


Output

num1=9
num2=10

The above example is of post-decrement operator.In that the value of num1 will be assigned first to num2 after that it will decrease the value of num1 by 1.So, the value of num1 will be 9 and num2 will also be 10.