Relational Operators in C Programming – Greater Than, Less Than, Equal To Operators Explained

program to use the less than operator (<) to compare the operand value in C.


#include <stdio.h>
int main ()
{
int num1=6, num2=7;

printf("\n num1 < num2=%d",(num1 < num2));
printf("\n num2 < num1=%d",(num2 < num1));
return(0);
}
Output:
num1 < num2= 1
num2 < num1 =0

Program Explanation:
This program serves as a simple example of the Less Than (<) relational operator to be used in C programming. This code is going to compare two integers num1 and num2 using this operator and print the result.

First two variables declared are num1 and num2 which are integers. Hardcoded values of 6 and 7 are given to num1 and num2 respectively. No user input would be expected since these values are hardcoded into the program.
First part compares num1 is less than num2 with the help of Less Than (<) operator; the statement printf("\n num1 < num2 = %d", (num1 < num2)); just does that. So the comparison will check whether 6 is less than 7, which is true. In C, such a true comparison is represented by 1. Hence, the output for this comparison should be as shown below: num1<num2=1.

The second comparison happens now: it checks whether num2 (which is 7) is less than num1 (which is 6) using the same Less Than operator. This is done by the line printf("\n num2 < num1 = %d", (num2 < num1));. Given that 7 is not less than 6, this returns false code-wise, which in C is represented by 0. So here the output is num2 < num1 = 0.
The program has finally executed successfully with the return value 0.

In conclusion, the program demonstrates the way in which the Less Than (<) relational operator functions in C. It compares two integers, num1 and num2 and prints whether these comparisons yield true (1) or false (0). In this specific case, since 6 is less than 7 the first comparison yields true (1) as opposed to the second which returns false (0).


Previous Topic:-->> increment decrement Operators || Next topic:-->>Logical perators