What is operator?
An operator is a symbol used to perform certain mathematical or logical operations. It is used in programs to manipulate data and variables.
Types of Operators in C
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and decrement operators
- Conditional operators
- Bitwise operators
- Special operators
If a & b are two integer variable are called Operands.
Examples: a-b, a+b, a*b, a/b, a%b
- if a & b are integer than after Arithmetic operation ,the result is also integer.
- if a & b are float or double than after Arithmetic operation ,the result is also float or double.
- The % modulo operator cannot used on floating point data.
Relational Operators are used to compare and check the relationship between two operands.
Example: comparing the age of two persons( 10<20 is true, 20<10 is false, 20!=20 is false ).
C has the following three logical operators.
EXAMPLES: if (11>10 && 15>16) is false, if (11>10 || 15>16) is true.
Assignment operators are used to assign the result of an expression to a variable (Right operands to its left operands).
Equal (‘=’) Operator is used as assignment operator.
The result of expression “5+4” i.e 9 will be assigned to variable x .
Conditional operators (? :) are ternary operators that takes three expressions as operand as shown below:
Syntax: exp1 ? exp2 : exp3
It evaluates exp2 when exp1 is True otherwise exp3 is evaluated .
Increment and Decrement operators
Increment & Decrement are Unary Operators that add or subtract, to or from its operand.
Examples: ++a, a++, a--, --a
For manipulation of data at bit level a special type of operators are used called Bitwise Operators.
We used it for shifting the bits right or left.
Note:Bitwise operator may not be applied to float or double. It takes only Integer as operand.
C support some special type of operators such as comma operator , sizeof operator , pointer operator (*) .
< m=sizeof (sum); // sizeof operator
int a,b; //comma operator
Precedence of operators
Precedence determines the order in which operators are evaluated in the given expression in absense of paraentheses.
Note: Paraentheses is used to override the operator precedence.
Here we use precedence operator rule as priority is given in table.
C operators are listed in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied .