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