An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions.
Example:
1 2 3 |
int c = a+b*5 |
Where, =, +,* are operators, a,b,c are the variables and 5 is the constants.
There are various types of the operator in C Language.
Types of Operator
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
- Increment/Decrement operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.
Operator | Example (int a=9, b=2) | Result |
+ | A+B | 11 |
– | A-B | 7 |
* | A*B | 18 |
/ | A/B | 4.5 |
% | A%4 | 1 |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int a=9,b=2, add,sub,mul,div,mod; add = a+b; sub = a-b; mul = a*b; div = a/b; mod = a%b; printf("Addition of (a+b ) is : %d\n", add); printf("Subtraction of (a-b) is : %d\n", sub); printf("Multiplication of (a*b) is : %d\n", mul); printf("Division of (a / b) is : %d\n", div); printf("Modulus of (a%b) is : %d\n", mod); } |
Output:
1 2 3 4 5 6 7 |
Addition of (a+b ) is : 11 Subtraction of (a-b) is : 7 Multiplication of (a*b) is : 18 Division of (a / b) is : 4 Modulus of (a%b) is : 1 |
Relational Operators
Relational operators are used to compare the value of two variables.
Operator | Description |
> | x > y (x is greater than y) |
< | x < y (x is less than y) |
>= | x >= y (x is greater than or equal to y) |
<= | x <= y (x is less than or equal to y) |
== | x == y (x is equal to y) |
!= | x != y (x is not equal to y) |
Let us consider an example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { int a=9,b=2; if (a == b) { printf("a and b are equal"); } else { printf("a and b are not equal"); } } |
Output
1 2 3 |
a and b are not equal |
Logical Operators
Logical operators are used to perform logical operations on the given two variables. There are 3 logical operators in C LanguageAND(&&), OR ( || ) and NOT (!).
Operator | Description |
&& | It returns true when both conditions are true. |
|| | It returns true while at least one condition is true. |
! | It reverses the state of the operand. |
Let us consider an example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int a=8, b=3, c=10; if(a>b && b<c){ printf("$$Operator : Both Condition are true \n"); } if(a>b || b<c){ printf("||Operator : Only one condition is true \n"); } return 0; } |
Output
1 2 3 4 |
$$Operator : Both Condition are true ||Operator : Only one condition is true |
Assignment operators
Assignment operators are used to assign the values for the variable.
Operator | Description |
= | sum = 10; 10 is assigned to variable sum |
+= | sum += 10;
This is same as sum = sum + 10 |
-= | sum -= 10;
It is same as sum = sum – 10 |
*= | sum *= 10;
It is same as sum = sum * 10 |
/= | sum /= 10;
It is same as sum = sum / 10 |
%= | sum %= 10;
It is same as sum = sum % 10 |
&= | sum&=10;
It is same as sum = sum & 10 |
^= | sum ^= 10;
It is same as sum = sum ^ 10 |
Let us consider an example
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main(){ int a=8,b=3,c; c=a+b; printf("a+b value is assign to c"); return 0; } |
Output
1 2 3 |
a+b value is assign to c |
Operator Precedence in C
The precedence of operators are used to specify the operators which will be evaluated first and next.
Example:
1 2 3 |
int result = 5+5*10-2; |
The precedence and associatively of C operators is given below:
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 |
Miscellaneous operators in c
There are few other important operators including sizeof and ?: supported by C Language.
Operator | Description |
sizeof() | It returns the size of a variable. |
& | It returns the address of a variable. |
* | It assigns pointer to a variable. |
? : | It is a Conditional Expression. |
Bitwise operators
Bitwise operator is used to perform bit by bit operations on two variables.
There are following truth table for &, | and ^.
P | q | p & q | p | q | p ^ q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Operator | Description |
& Binary AND Operator | It copies a bit to the result if it exists in both operands. |
| Binary OR Operator | It copies a bit if it exists in either operand. |
^ Binary XOR Operator | It copies the bit if it is set in one operand but not both. |
~ Binary Ones Complement Operator | It is unary and has the effect of ‘flipping’ bits. |
<< Binary Left Shift Operator | The left operands value is moved left by the number of bits specified by the right operand. |
>> Binary Right Shift Operator. | The left operands value is moved right by the number of bits specified by the right operand. |
Increment / decrement operator
Increment Operator:
The increment operator is used to increase the value of the variable by one.
Syntax:
1 2 3 |
Increment operator: ++ var_name; or var_name++; |
Example:
1 2 3 |
Increment operator: ++i; i++; |
Decrement Operator:
Decrement operator is used to decrease the value of the variable by one.
Syntax:
1 2 3 |
Decrement operator: -- var_name; or var_name--; |
Example:
1 2 3 |
Decrement operator: --i; i--; |
Difference between Increment & Decrement operators
There are various differences between pre/Post increment & decrement operators in C.
Operator | Operator/Description |
Pre increment operator (++i) | The value of i is incremented before assigning it to the variable i. |
Post increment operator (i++) | The value of i is incremented after assigning it to the variable i. |
Pre decrement operator (- -i) | The value of i is decremented before assigning it to the variable i. |
Post decrement operator (i–) | The value of i is decremented after assigning it to variable i. |