An operator is a symbol that helps us to perform specific mathematical and logical computation operation on a value or a variable.
There are following types of operators to perform different types of operations in C language.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators
Arithmetic Operator
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc
| Operator | Description | Example |
|---|---|---|
| + | Adds two operands. | A + B = 30 |
| − | Subtracts second operand from the first. | A − B = -10 |
| * | Multiplies both operands. | A * B = 200 |
| / | Divides numerator by de-numerator. | B / A = 2 |
| % | Modulus Operator and remainder of after an integer division. | B % A = 0 |
| ++ | Increment operator increases the integer value by one. | A++ = 11 |
| — | Decrement operator decreases the integer value by one. | A– = 9 |
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making.
| Operator | Meaning of Operator | Example |
|---|---|---|
| == | Equal to | 6 == 4 is evaluated to 0 |
| > | Greater than | 6 > 4 is evaluated to 1 |
| < | Less than | 6 < 4 is evaluated to 0 |
| != | Not equal to | 6 != 4 is evaluated to 1 |
| >= | Greater than or equal to | 6 >= 4 is evaluated to 1 |
| <= | Less than or equal to | 6 <= 4 is evaluated to 0 |
Logical Operator
Logical operators are used in decision making in C programming. An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
| Operator | Description | Example |
|---|---|---|
| && | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
| || | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
| ! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Bitwise Operator
The Bitwise operators is used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands.
| Operators | Meaning of operators |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise exclusive OR |
| ~ | Bitwise complement |
| << | Shift left |
| >> | Shift right |
Assignment Operator
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
Other Operators
It includes sizeof () and Ternary operators, Comma Operator.
sizeof() is a unary operator that returns the size of data (constants, variables, array, structure, etc.
Comma acts as both operator and separator.comma operator has the lowest precedence of any C operator. Comma operators are used to link related expressions together.
&:- Returns the address of a variable.
Ternary operators / Conditional Operator- ?
Operators Precedence in C
| Category | Operator | Associativity |
|---|---|---|
| Postfix | () [] -> . ++ – – | Left to right |
| Unary | + – ! ~ ++ – – (type)* & sizeof | Right to left |
| Multiplicative | * / % | Left to right |
| Additive | + – | Left to right |
| Shift | << >> | Left to right |
| Relational | < <= > >= | Left to right |
| Equality | == != | Left to right |
| Bitwise AND | & | Left to right |
| Bitwise XOR | ^ | Left to right |
| Bitwise OR | | | Left to right |
| Logical AND | && | Left to right |
| Logical OR | || | Left to right |
| Conditional | ?: | Right to left |
| Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
| Comma | , | Left to right |