What are Unary Operators in C?

By Anjnee Bhatnagar|Updated : October 12th, 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. This single operand is the only input used to create a new value.

Unary Operators in C PDF

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.

Download Formulas for GATE Computer Science Engineering - Programming & Data Structures

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, as per the GATE CSE syllabus, are classified based on the following criteria:

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

Download Formulas for GATE Computer Science Engineering - Discrete Mathematics

What is a Unary Operator in C?

Based on the number of operands an operator operates in the C programming language, the operators are classified as unary, binary, and ternary. Let us discuss each one for the upcoming GATE CSE exam 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. These are different from the modulus operators in C. 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.

Download Formulas for GATE Computer Science Engineering - Algorithms

Unary Operator in C Symbol

Check out the symbols of the various Unary Operator in C important for the GATE exam in the table provided below. These symbols are widely used in constructing a program. 

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

Types of Unary Operators 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 important as per the GATE question paper perspective, 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 first decreases the operator's value and 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.

Important GATE Topics

FDD Full FormResistors In Parallel
ISP Full FormResistors In Series
Stress And StrainProperties Of Gases
IEEE Full FormEnvironmental Studies Mcqs
Two Port NetworkFixed Beams

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