Operator
An operator is a symbol that helps to do certain mathematical calculations or logical manipulations or actions on one or more operands. An operand is something that an operator acts on. Eg +, *, >, <= etc.
On the basis of operands, operators are categorized into different types
- Binary Operators: Those operators that require two operands are binary operators. Eg. in X+Y, where X and Y are two operands and + is the operator.
- Unary Operators: Those operators that require one operand is unary operators. Eg. in ++X, Where X is one operand and ++ is the operator.
- Ternary Operators: Those operators that require three operands are ternary operators. Eg. in Expression1? Expression2: Expression3, Where? : is the ternary operator that required three operands which are Expression1, Expression2, and Expression3.
Different types of operators in C
Arithmetic Operators
Those operators which are used to perform mathematical calculations such as Addition, Subtraction, Multiplication, etc. It takes two operands and returns the result of the mathematical calculation. Eg +, -, *, /, %
Relational Operators
Those operators which are used to determine the relationship between the two operands. It takes two operands and returns the result of the relationship between them. Eg >, <, >=, <=, ==, !=
Assignment Operators
The assignment operator is used to assign a value or result of an expression to a variable. Eg x=2+3, where x is a variable and 2+3 is an expression. here the value of 2+3 is evaluated which is 5 and this 5 is assigned to the variable x.
Logical Operators
Logical Operators are used to give logical value that is either true or false. It is used to evaluate logical and relational expressions. Eg. &&(Logical AND), ||(Logical OR) and !(Logical NOT).
Increment and Decrement Operators
These operators are also called unary because they act upon the single operand. The increment operator(++) causes its operand to be increased by one, whereas the decrement operator (–) causes its operand to be decreased by one. Eg. ++, — (In a++, where ++ is the increment operator which is used to increase the value of a by one that is the above expression is equivalent to a=a+1)
Conditional Operators (Ternary Operator)
An operator (?:) that stores a value depending upon a condition. This operator is called a ternary operator as it requires three operands.
Syntax:
(Condition)?Statement1: Statement2;
In this conditional operator, at first the condition is checked and if it is found true then statement 1 will be executed otherwise statement 2 will be executed.
Bitwise Operators
Bitwise operators are used to performing calculations using binary digits which can be performed using the following operators. Eg. &(Bitwise AND), |(Bitwise OR), ^(Bitwise XOR), ~(Bitwise Complement), << (Left Shift), >>(Right Shift).
The Comma Operator
The comma operator can be used to link related expressions together. Comma-linked lists of expressions are evaluated left to right and the value of the right-most expressions is the value of the combined expression.
z = (x=5, y=2, x-y);
first 5 assigns to x and 2 assigns to y and finally, 3 assigns to z.