Operators are symbols that help in performing operations of mathematical and logical nature.The classification of C operators are as follows
- Arithmetic
- Relational
- Logical
- Bitwise
- Assignment
- Conditional
Uses of Operators in C:-
The operators basically serve as symbols that operate on any value or variable. We use it for performing various operations- such as logical, arithmetic, relational, and many more. A programmer must use various operators for performing certain types of mathematical operations. Thus, the primary purpose of the operators is to perform various logical and mathematical calculations.
The programming languages like C come with some built-in functions that are rich in nature. The use of these operators is vast. These operators act as very powerful and useful features of all the programming languages, and the functionality of these languages is pretty much useless without these. These make it very easy for programmers to write the code very easily and efficiently.
1. Arithmetic Operators
C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.
Operators | Function |
---|---|
+ (Addition) | Adds to Values |
– (Subtraction) | Subtract a second value from first |
* (Multiply) | Multiply Two values |
/ (Division) | Divide numerator by the denominator |
% (Modulus) | Remainder of Division |
Examples:-
#include <stdio.h> int main() { int a=40,b=20, 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:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
2. Relational Operators
When we want to compare the values of two operands, then relational operators are used. If we want to check that is one operand is equal to or greater than other operands, then we use >= operator.
The below table lists of the relational operators in C with their functions.
Operator | Function | Example |
---|---|---|
== | This will check if two operands are equal | 6 == 2 returns 0 |
!= | This will check if two operands are not equal | 6 != 2 returns 1 |
> | This will check if the operand on the left is greater than operand on the right | 6 > 2 returns 1 |
< | This will check if the operand on the left is smaller than the right operand | 6 < 2 returns 0 |
>= | This will check if the left operand is greater than or equal to the right operand | 6 >= 2 returns 1 |
<= | This will check if operand on left is smaller than or equal to the right operand | 6 <= 2 return 0 |
Example:
// Working of arithmetic operators #include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d \n",c); c = a-b; printf("a-b = %d \n",c); c = a*b; printf("a*b = %d \n",c); c = a/b; printf("a/b = %d \n",c); c = a%b; printf("Remainder when a divided by b = %d \n",c); return 0; }
Output:
a+b = 13 a-b = 5 a*b = 36 a/b = 2 Remainder when a divided by b=1
3.Logical Operators
Logical Operators are used for True or False results.
Operator | Function | Example(if a=1 & b=0) |
---|---|---|
&& | Logical AND | (a && b) is false |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is false |
Example:
#include <stdio.h> int main() { int a = 10, b = 4, c = 10, d = 20; // logical operators // logical AND example if (a > b && c == d) printf("a is greater than b AND c is equal to d\n"); else printf("AND condition not satisfied\n"); // logical OR example if (a > b || c == d) printf("a is greater than b OR c is equal to d\n"); else printf("Neither a is greater than b nor c is equal " " to d\n"); // logical NOT example if (!a) printf("a is zero\n"); else printf("a is not zero"); return 0; }
Output:
AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero
4.Bitwise Operators:
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
Operators | Meaning of Operators |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise Exclusive OR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift right |
5.Assignment Operators
These types of operators are used to assign a value to a variable.
Operator | Example |
---|---|
= | a=b |
+= | a+=b is same as a=a+b |
-= | a-=b is same as a=a-b |
*= | a*=b is same as a=a*b |
/= | a/=b is same as a=a/b |
%= | a%=b is same as a=a%b |
Example:-
// Working of assignment operators #include <stdio.h> int main() { int a = 5, c; c = a; // c is 5 printf("c = %d\n", c); c += a; // c is 10 printf("c = %d\n", c); c -= a; // c is 5 printf("c = %d\n", c); c *= a; // c is 25 printf("c = %d\n", c); c /= a; // c is 5 printf("c = %d\n", c); c %= a; // c = 0 printf("c = %d\n", c); return 0; }
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0