This article is about what is an operator and various operators in C programming.
A program consists of different operations to meet the requirements. An operator is a symbol that tells the compiler to perform the specific mathematical or logical functions between the operands.
For example In the operation ‘a+b’ ‘a’ and ‘b’ are the operands and ‘+’ is the operator.
There are different types of operators in C. They are as follows:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
The operators which perform arithmetic operations are called arithmetic operators (Ex. Addition, subtraction, multiplication, division, etc.) Example
- A + B = 30
- A – B = 10
- A * B = 200
- A / B = 2
- B % A = 0 (gives the remainder after a division)
A relational operator is an operator that tests or defines some kind of relation between two entities.
- A > B (greater than)
- A < B (lesser than)
- A >= B (greater than or equal to)
- A <= B (lesser than or equal to)
- A == B (equal to)
- A != B (not equal to)
The logical operator is meant to manipulate Boolean values only.
- && - AND operator (returns true when both operands are non-zero)
- || - OR operator (returns true if any one of the operands is non-zero)
- ! - NOT operator (used to reverse the state of the operand)
• Bitwise operator works on the principle of a Boolean theorem. • The truth tables for ‘&’(AND), ‘|’(OR), and ‘^’(NOT) is as follows :
An assignment operator is the one that assigns values to the operands.
- = Simple assignment operator.
- += Add AND assignment operator.
- -= Subtract AND assignment operator.
- *= Multiply AND assignment operator.
- /= Divide AND assignment operator.
- %= Modulus AND assignment operator.
- <<= Left shift AND assignment operator.
- =>> Right shift AND assignment operator.
- &= Bitwise AND assignment operator.
- ^= Bitwise exclusive OR and assignment operator.
- |= Bitwise inclusive OR and assignment operator.
Miscellaneous Operators (sizeof & ternary)
Certain other operators are supported by the C language. They include ‘sizeof’ and ‘ternary’.
- sizeof() Returns the size of a variable.
- & Returns the address of a variable.
- Pointer to a variable.
- ? : Conditional Expression.
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides which expression is evaluated first and followed by. Certain operators have higher precedence than others; for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.
To be simple, the operator precedence follows the ODMAS order. ODMAS stands for ‘Order of Division Multiplication Addition and Subtraction’.
For example, x = 5 + 4 * 3; here, x is assigned 17, not 27 because operator * has higher precedence than +, so it first gets multiplied with 4*3 and then adds into 5. Note: Precedence rules can be overridden by explicit parentheses in the expression.