Introduction to C Programming
Operators in C Part 1
Operators in C Part 2
Precedence and Associativity Of Operators in C
If else Statements in C
Switch Statement in C
Loops in C
While and Do-While loops in C
Nested loops in C
Loop Control Statements in C
1D Arrays in C
2D Arrays in C
Pattern Printing in 2D arrays
Functions in C
Pointers in C
ASCII Encoding and Typecasting in C
Operators Part 2
- Comparison Operators
- Logical Operators
- Bitwise Operators
Jump to specific sections
Comparison operators
- Equals to Operator (a == b) - Returns true if a and b are equal.
- Not equals to Operator (a != b) - Returns true if a is not equal to b.
- Greater than Operator (a > b) - Returns true if a is greater than b
- Less than Operator (a < b) - Returns true if a is smaller than b
- Greater than equals to Operator (a >= b) - Returns true if a is greater than or equal to b
- Greater than Operator (a <= b) - Returns true if a is less than or equal to b
#include <stdio.h>
void main()
{
printf("Result of 5==5 = %d \n",5==5);
printf("Result of 6!=5 = %d \n",6!=5);
printf("Result of 6>3 = %d \n",6>3);
printf("Result of 4<1 = %d \n",4<1);
printf("Result of 9>=9 = %d \n",9>=9);
printf("Result of 3<=8 = %d \n",3<=8);
}
Output
Result of 5==5 = 1
Result of 6!=5 = 1
Result of 6>3 = 1
Result of 4<1 = 0
Result of 9>=9 = 1
Result of 3<=8 = 1
Logical Operators
- AND Operator(&&) - Returns true if both the conditions are true.
- OR Operator(&&) - Return true if any one condition is true.
- AND Operator(&&) - Returns true if the condition is false.
#include <stdio.h>
void main()
{
int a=5,b=6,c=12;
printf("Result of (a>b && c>a) = %d \n",a>b && c>a);
printf("Result of (c>b || b>a) = %d \n",c>b || b>a);
printf("Result of (a>b) = %d \n",!(a>b));
}
Output
Result of (a>b && c>a) = 0
Result of (c>b || b>a) = 1
Result of (a>b) = 1
Bitwise Operators
Bitwise operators are used to manipulate the numbers at the bit level. They are of two types -
1. Left Shift(<<) - Shifts a number to the left by 1 bit by adding 0 to LSB. MSB gets lost. Left shifting a number(n) r times multiplies n with the value 2 r.
2. Right Shift(>>) - Shifts a number to the right by 1 bit by adding 0 to MSB. LSB gets lost. Right shifting a number(n) r times divides n by the value 2 r.
1. Left Shift(<<) - Shifts a number to the left by 1 bit by adding 0 to LSB. MSB gets lost. Left shifting a number(n) r times multiplies n with the value 2 r.
2. Right Shift(>>) - Shifts a number to the right by 1 bit by adding 0 to MSB. LSB gets lost. Right shifting a number(n) r times divides n by the value 2 r.
#include <stdio.h>
void main()
{
int a = 4;
printf("Result of a<<1 = %d \n",a<<1);
printf("Result of a<<2 = %d \n",a<<2);
printf("Result of a>>1 = %d \n",a>>1);
printf("Result of a>>1 = %d \n",a>>2);
}
Output
Result of a<<1 = 8
Result of a<<2 = 16
Result of a>>1 = 2
Result of a>>2 = 1