What are Unary Operators in C? - Details, Types, Symbol

By Anjnee Bhatnagar|Updated : August 26th, 2022

Unary Operator in C operates on one operand only. An operator specifies the operation to be applied on operands to get the desired result. In this single operand as the only input is used to create a new value.

This article will discuss 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 an operator means and the type of operators available in C.

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 based on the following criteria:

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

What is Unary Operator in C?

In C, based on the number of operands an operator operates, the operators are classified as unary, binary, and ternary. 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 of Unary Operator in C

Having discussed the unary operator 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 decreases the operator's value 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 decreases the operator's 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=5.

Unary Operator in C Symbol

Check out the symbols of the various Unary Operator in C in the table provided below:

Unary OperatorSymbol
Prefix Increment Operator++a
Post Increment Operatora++
Unary plus+
Unary Minus-
Prefix Decrement Operator--a
Post Decrement Operatora--

Comments

write a comment

FAQs on Unary Operators in C

  • 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