In this article we are going to learn different operators in C, so lets start.
An operator is a symbol that works on the variable or on the values of the variable, which directs the compiler what is to be done. These are used in specific mathematical and logical operations.
There are different types of operators in C, they are:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operator
- Assignment Operator
- Misc. Operators
Let us discuss them one by one.
Arithmetic Operators:
Such kind of operators performs all kind of mathematical operations, they are:
Operators | Name | Meaning |
+ | Addition | Adds two operands |
– | Subtraction | Subtracts second operand from the first |
* | Multiplication | Multiplies both the operands |
/ | Division | Divides numerator by de-numerator |
% | Modulo | Divides and returns the remainder value |
Example illustrating the arithmetic operators:
#include<stdio.h> int main() { int a=20,b=10; int res=a+b;//addition operator printf("The addition operator=%d",res); int res1=a-b;//subtraction operator printf("\nThe subtraction operator=%d",res1); int res2=a*b;//multiplication operator printf("\nThe multiplication operator=%d",res2); int res3=a/b;//division operator printf("\nThe division operator=%d",res3); int res4=a%b;//modulo operator printf("\nThe modulo operator=%d",res4); return 0; }
Output:
Relational Operator:
As the name suggests a relational operator shows the relation between two operands. The following table shows all the relational operator.
Operator | Name | Meaning |
== | Equal to | Checks if the values of the two operands are equal or not, true when equal. |
!= | Not equal to | Checks if the values of the two operands are equal or not, true when not equal. |
> | Greater than | Checks which is greater among two operands. |
< | Less than | Checks which is smaller among two operands. |
>= | Greater than equals to | Checks which is greater or equal to among two operands. |
<= | Less than equals to | Checks which is smaller or equal to among two operands. |
An example illustrating the relational operator:
#include <stdio.h> int main() { int a = 10, b = 20, c = 10; printf("%d == %d is %d \n", a, b, a == b); printf("%d == %d is %d \n", a, c, a == c); printf("%d > %d is %d \n", a, b, a > b); printf("%d > %d is %d \n", a, c, a > c); printf("%d < %d is %d \n", a, b, a < b); printf("%d < %d is %d \n", a, c, a < c); printf("%d != %d is %d \n", a, b, a != b); printf("%d != %d is %d \n", a, c, a != c); printf("%d >= %d is %d \n", a, b, a >= b); printf("%d >= %d is %d \n", a, c, a >= c); printf("%d <= %d is %d \n", a, b, a <= b); printf("%d <= %d is %d \n", a, c, a <= c); return 0; }
Output:
Logical Operator:
The operators used to evaluate two or more expressions are called logical operators. The following table shows the operators:
Operators | Name | Meaning |
&& | Logical and | True if both the operands are non-zero |
|| | Logical or | True if any one of the operand is non-zero |
! | Logical not | Reverse the logical state of its operand |
An example illustrating the logical operator:
#include <stdio.h> int main() { int a=10, b=20, c=10, res; res=(a==b) && (c>b); printf("(a == b) && (c > b) is %d \n", res); res=(a==b) && (c<b); printf("(a == b) && (c < b) is %d \n", res); res=(a==b) || (c<b); printf("(a == b) || (c < b) is %d \n", res); res=(a!=b) || (c<b); printf("(a != b) || (c < b) is %d \n", res); res=!(a!=b); printf("!(a != b) is %d \n", res); res=!(a==b); printf("!(a == b) is %d \n", res); return 0; }
Output:
Bitwise Operator:
This operator works bit by bit and performs bit by bit operation. The following table shows the bitwise operators:
Operators | Name |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
~ | Bitwise Complement |
<< | Shift left |
>> | Shift right |
An example illustrating a bitwise operator:
//example of bitwise operator #include<stdio.h> int main() { int a=6,b=4; printf("(a&b)=>%d\n",a&b); printf("(a|b)=>%d\n",a|b); printf("~(a)=>%d\n",~a); printf("~(b)=>%d\n",~b); printf("(a>>2)=>%d\n",a>>2); printf("(a<<2)=>%d\n",a<<2); printf("(b>>2)=>%d\n",b>>2); printf("(b<<2)=>%d\n",b<<2); return 0; }
Output:
#Ouutput (a&b)=>4 (a|b)=>6 ~(a)=>-7 ~(b)=>-5 (a>>2)=>1 (a<<2)=>24 (b>>2)=>1 (b<<2)=>16
Assignment Operator:
This operator is used to assign value to a variable. The table shows the assignment operators:
Operator | Name |
= | Assignment operator |
+= | Add AND assignment operator |
-= | Subtract AND assignment operator |
*= | Multiply AND assignment operator |
/= | Divide AND assignment operator |
%= | Modulus AND assignment operator |
Example illustrating the assignment operator:
#include <stdio.h> int main() { int a = 5, c; c = a; printf("c = %d\n", c); c += a; printf("c = %d\n", c); c -= a; printf("c = %d\n", c); c *= a; printf("c = %d\n", c); c /= a; printf("c = %d\n", c); c %= a; printf("c = %d\n", c); return 0; }
Output:
Misc. Operators:
This operator contains the other operators such as the increment, decrement and sizeof operators. The table below shows the operators:
Operators | Name |
++ | Increment operator |
— | Decrement operator |
sizeof | Sizeof operator |
Example illustrating the misc. operators:
#include <stdio.h> int main() { int a = 10, a1 = 20; float b; double c; char d; printf("++a = %d \n", ++a); printf("--b = %d \n", --a1); printf("Size of int=%lu bytes\n",sizeof(a)); printf("Size of float=%lu bytes\n",sizeof(b)); printf("Size of double=%lu bytes\n",sizeof(c)); printf("Size of char=%lu byte\n",sizeof(d)); return 0; }
Output:
Here we end our discussion on operators in C, hope u like it and have cleared all your doubts related to this topic.
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.