In C Programing language sometimes we need to compare two values.
For example, we are evaluating students' marks, and we have to check if the student passes or fails?
The pass mark is 23 out of 70, below the 23 mark will count as a failure.
How to check?
Student Marks Obtained Result
-----------------------------------------
1. Student1 45 Pass
2. Student2 23 Pass
3. Student3 21 Fail
Condition is:- if(marks >= 23) {
//Pass
}
else{
//Fail
}
To do this type of comparison/Operation we have Relational Operators. We have 6 Operators here given below.
- == Compares both side values, returns true if both are equal
- != Compares both side values returns true if both are not equal
- > Checks if the left side value is greater than the right side value
- < Checks if the left side value is less than the right side value
- >= Checks if the left side value is greater than or equal to the right side value
- <= Checks if the left side value is less than or equal to the right side value
== Equal to Operator
if(Operand1 == Operand2) { //true } else{ //false }
!= Not Equal to Operator
if(Operand1 != Operand2) { //true } else{ //false }
> Greater Than Operator
if(Operand1 > Operand2) { //true } else{ //false }
< Less Than Operator
if(Operand1 < Operand2) { //true } else{ //false }
>= Greater Than Equal to Operator
if(Operand1 >= Operand2) { //true } else{ //false }
<= Less Than Equal to Operator
if(Operand1 <= Operand2) { //true } else{ //false }
Let's use these operands on code, run it and see the outputs by providing different inputs.
#include <stdio.h>}int main(){int num1,num2;printf("Enter 1st Number:");scanf("%d",&num1);printf("Enter 2nd Number:");scanf("%d",&num2);if(num1==num2){printf("num1 is equal to num2 \n");}if(num1!=num2){printf("num1 is not equal to num2 \n");}if(num1>num2){printf("num1 is greater than num2 \n");}if(num1<num2){printf("num1 is less than num2 \n");}if(num1>=num2){printf("num1 is greater than equal to num2 \n");}if(num1<=num2){printf("num1 is less than equal to num2 \n");}return 0;