What are Unary Operators in C?

By Anjnee Bhatnagar|Updated : May 10th, 2022

Unary Operators in C: An operator specifies the operation to be applied on operands to get the desired result. The unary operators in C operate on one operand only. In this article, we will discuss in-depth the meaning of unary operators in C, the types of unary operators in C, and the functions of unary operators in C.

Before jumping onto the unary operators in C, let us first discuss what is meant by an operator and the type of operators available in C.

Table of Content

What is an Operator?

An operator specifies the operation to be applied to its operands. An operand specifies an entity on which an operation is to be performed. An operand can be a variable name, a constant, a function call, or a macro name. The operators in C are classified on the basis of the following criteria:

  • The number of operands on which an operator operates.
  • The role of an operator.

What are unary operators in C?

In C, based upon the number of operands on which an operator operates, the operators are classified as unary operators, binary operators, and ternary operators. Let us discuss each one separately:

  • Unary Operator: A unary operator operates on only a single operand. For example in the expression -8, “-“ is a unary minus operator as it operates on only one operand. The operand can be present towards the right of the unary operator. Example of unary operators are:- &(address-of operator), size of operator, !(logical negation), ~(bitwise negation), ++(increment operator), --(decrement operator), unary +, unary -, etc.
  • Binary Operators: A binary operator operates on two operands. It requires an operand on both sides that are left and right. For example in the expression 3+9, “+” is a binary operator with 3 and 9 as its left and right operands respectively. Examples of binary operators are:- *(multiplication), %(modulus operator), ==(equality operator), &&(logical AND operator), &(Bitwise AND operator), etc.
  • Ternary operator: operates on three operands. The conditional operator(? :) is the only ternary operator available in C.

Types and Examples of Unary Operators in C

Having discussed the unary operators in C and various other types of operators based on the number of operands, let us now see a few unary operators along with examples:

  • Prefix Increment Operator: It increments the value of the operator first and then stores it in the memory. In this, the operator is present on the left side of the operand. For instance int a=6; int b=++a; // b=7, a=7.
  • Post Increment Operator: It increments the value of the operator value after storing it in the memory. In this, the operator is present on the right side of the operand. For instance int a=6; int b=a++; // b=6, a=7.
  • Unary plus: It just signifies that a number is positive. The operand is an arithmetic type. Its result is the value of the operand. For example: int a= +8.
  • Unary minus: It just signifies that a number is negative. The operand is an arithmetic type. Its result is the value of the operand. For example: int a= -8.
  • Prefix Decrement Operator: It decrements the value of the operator first and then stores it in the memory. In this, the operator is present on the left side of the operand. For instance int a=6; int b=--a; // b=5, a=5.
  • Post Decrement Operator: It decrements the value of the operator, after storing it in the memory. In this, the operator is present on the right side of the operand. For instance int a=6; int b=a--; // b=6, a=5.

Comments

write a comment

FAQs

  • A unary operator operates on only one operand. For example in the expression -8, “-“ is a unary minus operator as it operates on only one operand. The operand can be present towards the right of the unary operator.

  • A binary operator operates on two operands. It requires an operand on both sides that are left and right. For example in the expression 3+9, “+” is a binary operator with 3 and 9 as its left and right operands respectively.

  • The operators in C are classified on the basis of the number of operands on which an operator operates. There are three types of operators namely unary operators, binary operators, and ternary operators.

  • On the basis of the role of operators, we have six types of operators: Arithmetic operators, Relational operators, logical operators, Bitwise operators, Assignment operators, and Miscellaneous operators.

  • The symbol for the unary minus operator is minus (-). To alter the sign of any positive value to a negative value, use the unary operator. Using the unary minus operator, a positive number becomes a negative number, and a negative number becomes a positive number.

Follow us for latest updates