In this lesson we are learning about below topics:
1. Arithmetic Operators.
2. Assignment Operators.
3. Relational Operators.
4. Logical Operators.
5. Bitwise operators.
6. Conditional Operators.
7. Increment and Decrement Operators.
1. Arithmetic Operators.
Below is the list of arithmetic operators:
Operator Operator Name Example
+ Addition 3 + 3 = 6
- Subtraction 5 - 2 = 3
* Multiplication 3 * 3 = 9
/ Division 9 / 3 = 3
% Modulus 15 % 2 = 1 [remainder]
Example: Program to demonstrate Arithmetic Operators:
#include<stdio.h>
int main()
{
int num_1 = 20;
int num_2 = 10;
printf("The addition of %d and %d is = %d \n", num_1, num_2, (num_1 + num_2) );
printf("The Subtraction of %d and %d is = %d \n", num_1, num_2, (num_1 - num_2) );
printf("The Multiplication of %d and %d is = %d \n", num_1, num_2, (num_1 * num_2) );
printf("The Division of %d and %d is = %d \n", num_1, num_2, (num_1 / num_2) );
printf("The Modulus of %d and %d is = %d \n", num_1, num_2, (num_1 % num_2) );
return 0;
}
Output:
The addition of 20 and 10 is = 30
The Subtraction of 20 and 10 is = 10
The Multiplication of 20 and 10 is = 200
The Division of 20 and 10 is = 2
The Modulus of 20 and 10 is = 0
2. Assignment Operators.
Assignment operators can be used to assign a value to a variable. There are some shorthand assignment operators. Means, if you want to add value 10 to variable “a”, then we normally would write “a = a + 10”. Instead of that we can write “a += 10”.
Below is the list of assignment operators:
Operator Example
= a = 10;
+= a += 10; or a = a + 10;
*= a *= 10; or a = a * 10;
/= a /= 10; or a = a / 10;
%= a %= 10; or a = a % 10;
-= a -= 10; or a = a - 10;
<<= a <<= 10; or a = a << 10;
>>= a >>= 10; or a = a >> 10;
>>>= a >>>= 10; or a = a >>> 10;
&= a &= 10; or a = a & 10;
^= a ^= 10; or a = a ^ 10;
!= a != 10; or a = a ! 10;
Example: Below is an example for assignment operators:
#include<stdio.h>
int main()
{
int num_1 = 10;
printf("The result of += operator is = %d\n", num_1+=10 );
printf("The result of *= operator is = %d\n", num_1*=10 );
printf("The result of /= operator is = %d\n", num_1/=10 );
printf("The result of %= operator is = %d\n", num_1%=10 );
printf("The result of -= operator is = %d\n", num_1-=10 );
printf("The result of <<= operator is = %d\n", num_1<<=10 );
printf("The result of >>= operator is = %d\n", num_1>>=10 );
printf("The result of &= operator is = %d\n", num_1&=10 );
printf("The result of ^= operator is = %d\n", num_1^=10 );
printf("The result of != operator is = %d\n", num_1!=10 );
return 0;
}
Output:
The result of += operator is = 20
The result of *= operator is = 200
The result of /= operator is = 20
The result of = operator is = 0
The result of -= operator is = -10
The result of <<= operator is = -10240
The result of >>= operator is = -10
The result of &= operator is = 2
The result of ^= operator is = 8
The result of != operator is = 1
3. Relational Operators.
Relational operators are used to check if two values are true or false.
If the expression return 0 then it is false, if it returns 1 then it is true.
Below are the list of relational operators and their result.
Assume a = 10, b = 20
Operator Operator Name Example Result
> Greater Than a > b 0
< Lesser Than a < b 1
>= Greater than or equal to a >= b 0
<= Lesser than or equal to a <= b 1
== Equal to a == b 0
!= Not equal to a != b 1
Example: Below is the example of relational operator:
#include<stdio.h>
int main()
{
int num_1 = 20;
int num_2 = 10;
printf("The result of %d > %d operator is = %d\n", num_1, num_2, (num_1 > num_2) );
printf("The result of %d < %d operator is = %d\n", num_1, num_2, (num_1 < num_2) );
printf("The result of %d >= %d operator is = %d\n", num_1, num_2, (num_1 >= num_2) );
printf("The result of %d <= %d operator is = %d\n", num_1, num_2, (num_1 <= num_2) );
printf("The result of %d == %d operator is = %d\n", num_1, num_2, (num_1 == num_2) );
printf("The result of %d != %d operator is = %d\n", num_1, num_2, (num_1 != num_2) );
return 0;
}
Output:
The result of 20 > 10 operator is = 1
The result of 20 < 10 operator is = 0
The result of 20 >= 10 operator is = 1
The result of 20 <= 10 operator is = 0
The result of 20 == 10 operator is = 0
The result of 20 != 10 operator is = 1
4. Logical Operators.
Logical operators are used to check if the relation between 2 expressions are true or false.
Below are different types of Logical Operators:
Assume a = 10, b = 20
Operator Operator Name Example Result
&& Logical AND 10 > 5 && 6 >3 1
|| Logical OR 10 > 5 && 6 <3 1
! Logical NOT 10 != 1 0
In Logical AND will return True if both of the expressions are true. If any one of the expression is false, then it will return false.
In Logical OR will return True if any one of the expressions are true. If any both of the expression is false, then it will return false.
In Logical NOT will return True if the expression is false and vice versa.
Example: Below is the example of relational operator:
#include<stdio.h>
int main()
{
int num_1 = 20;
int num_2 = 10;
printf("The result of (num_1 > 30) && (num_2 > 5) operator is = %d\n", (num_1 > 30) && (num_2 > 5) );
printf("The result of (num_1 > 30) || (num_2 > 5) operator is = %d\n", (num_1 > 30) || (num_2 > 5) );
return 0;
}
Output:
The result of (num_1 > 30) && (num_2 > 5) operator is = 0
The result of (num_1 > 30) || (num_2 > 5) operator is = 1
5. Bit wise operators.
Bit wise operators are used for bit manipulation. They can only be applied to int, char, short and long data types.
Below is the list of bitwise operators:
Operator Operator Name
>> Right shift
<< Left Shift
^ Bitwise XOR
~ One’s complement
& Bitwise AND
| Bitwise OR
Example: Below is an example for Bitwise operators:
#include<stdio.h>
int main()
{
int num_1 = 20;
int num_2 = 10;
printf("The result of %d>> 2 is = %d\n", num_1, num_1>>2 );
printf("The result of %d << 2 is = %d\n", num_1, num_1<<2 );
printf("The result of %d & %d is = %d\n", num_1, num_2, num_1 & num_2 );
printf("The result of %d | %d is = %d\n", num_1, num_2, num_1 | num_2 );
printf("The result of %d ^ %d is = %d\n", num_1, num_2, num_1 ^ num_2 );
return 0;
}
Output:
The result of 20>> 2 is = 5
The result of 20 << 2 is = 80
The result of 20 & 10 is = 0
The result of 20 | 10 is = 30
The result of 20 ^ 10 is = 30
6. Conditional Operator.
Conditional operators are shorthand for if-else statement. They are also called as ternary operators.
This operator consists of 3 parts. Condition part followed by 2 expression or values, first is executed if the condition is true other is executed if condition is false.
Syntax:
Condition : (expression to be executed if true) ? (expression to be executed if false);
Example:
#include<stdio.h>
int main(int argc, char const *argv[])
{
printf("is 4 > 5 ? = %s \n", ( (4 > 5) ? "True" : "False"));
return 0;
}
Output:
is 4 > 5 ? = False
7. Increment and Decrement Operators.
They are also called as Unary Operators. Because they operate on only one operand.
++ is Increment Operator
— is Decrement Operator
++num is pre-increment. Here value will be incremented then will be assigned to num.
num++ is post-increment. Here value will be assigned then it will be incremented.
Below is the example for the same:
#include<stdio.h>
int main()
{
int num = 1;
printf("performing pre increment ++%d = %d \n",num, ++num );
//as we have incremented the value above from 1 to 2, here the num value will be 2
printf("performing post increment %d++ = %d \n",num, num++ );
//as we have incremented the value above from 2 to 3, here the num value will be 3.
printf("performing pre decrement --%d = %d \n",num, --num );
//as we have decremented the value above from 3 to 2, here the num value will be 2.
printf("performing post decrement %d-- = %d \n",num, num-- );
return 0;
}
Output:
performing pre increment ++1 = 2
performing post increment 2++ = 2
performing pre decrement --3 = 2
performing post decrement 2-- = 2