Article by: Manish Methani
Last Updated: November 7, 2021 at 2:04pm IST
Operators are an essential part of any programming language, and C programming language is no exception. Operators are symbols that represent a specific operation that can be performed on one or more operands. In C, operators are classified into different types based on their functionality. In this tutorial, we will explore everything about operators in the C programming language with examples.
Arithmetic operators are used for mathematical calculations. The following are the arithmetic operators in C:
Here is an example of using arithmetic operators in C:
#include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d",c); c = a-b; printf("a-b = %d",c); c = a*b; printf("a*b = %d",c); c = a/b; printf("a/b = %d",c); c = a%b; printf("Remainder when a divided by b = %d",c); return 0; }
Output:
a+b = 13 a-b = 5 a*b = 36 a/b = 2 Remainder when a divided by b = 1
Relational operators are used to comparing two values. The following are the relational operators in C:
Here is an example of using relational operators in C:
#include <stdio.h> int main() { int a = 10, b = 4; printf("a > b is %d", a > b); printf("a < b is %d", a < b); printf("a >= b is %d", a >= b); printf("a <= b is %d", a <= b); printf("a == b is %d", a == b); printf("a != b is %d", a != b); return 0; }
Output:
a > b is 1 a < b is 0 a >= b is 1 a <= b is 0 a == b is 0 a != b is 1
Logical operators are used to combining two or more conditions. The following are the logical operators in C:
Here is an example of using logical operators in C:
#include <stdio.h> int main() { int marks = 65; if(marks > 75 && marks < 99) { printf("He is topper"); } else if(marks < 75 && marks >= 65) { printf("He cracked the exam"); } else { printf("He failed"); } return 0; }
Output:
He cracked the exam
Bitwise operators are used to perform bitwise operations on two integer operands. The following are the bitwise operators in C:
There are many Bitwise operators in C as defined below:
& (bitwise AND):- Performs the AND Operation bit by bit. The result of AND is 1 only if both bits are 1.
| (bitwise OR):- Performs the OR Operation bit by bit. The result of OR is 1 any of the two bits is 1.
^ (bitwise XOR):- Performs the XOR Operation bit by bit. The result of XOR is 1 if the two bits are different.
<< (left shift):- Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
>> (right shift):- Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
~ (bitwise NOT):- Takes one number and inverts all bits of it.
Here is an example of using bitwise operators in C:
#include <stdio.h> int main() { int a = 5, b = 9, result; result = a & b; printf("Bitwise AND: %d ", result); result = a | b; }
Output:
Bitwise AND: 1
An assignment operator is used for assigning a value to a variable.
Operator | Example | Same as |
---|---|---|
= | a = b | a = b |
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
%= | a %= b | a = a%b |
C program to illustrate the concept of Assignment Operators
#include <stdio.h> int main() { int a; a+= 10; printf("a = %d",a); a -= 2; printf("a = %d",a); a *= 2; printf("a = %d",a); a /= 2; printf("a = %d",a); a %=2; printf("a = %d",a); return 0; }
Output
a = 10 a = 8 a = 16 a = 8 a = 0
There are various scenarios where the compiler has to know which operation should perform first. What do you think this expression will be evaluated according to the compiler?
2 * x - 3 * y
Now,
There are two ways to solve this equation
1) (2x)-(3y) 2) 2(x-3y)
We engineers know how to solve these problems because we know multiplication is solved first then minus operation should be applied. But how does the compiler know how to solve this? So Hierarchy of Operations helps the compiler to understand this kind of equations.
Priority | Operators | Description |
---|---|---|
1st | */% | multiplication, division, modular division |
2nd | +- | addition, subtraction |
3rd | = | assignment |
* will be always solved first then the division (/) operator. + will be solved before the - operator but * will be solved first then the + operator.
a = 2*3/4+4/4
As * has max priority than "/" So it will be evaluated first. So,
6/4+4/4
Now "/" has max priority than "+" so
1 + 4/4
Now "/" has max priority than "+" so
1 + 1
Result is 2