Using Relational Operators in C: A Beginner's Guide


A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational Operators are a bunch of binary operators that are used to check for relations between two operands including equality, greater than, less than, etc. They return a boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements and so on.

Operatoruses
> greater than operator
< less than operator
<= less than or equal to operator
>= greater than or equal to operator
== equality operator

The code is a C program that demonstrates the use of relational operators in C. Here is an explanation of each line of the code:

  • int a=10,b=5; declares two variables a and b of type int and assigns them the values 10 and 5 respectively.
  • printf("Greater Than : %d",a>b); uses the relational operator '>' to check if the value of a is greater than b, if yes then it returns 1 otherwise 0. This is printed in the console.
  • printf("\nLess Than : %d",a < b); uses the relational operator '<' to check if the value of a is less than b, if yes then it returns 1 otherwise 0. This is printed in the console.
  • printf("\nGreater Than or Equal : %d",a>=b); uses the relational operator '>=' to check if the value of a is greater than or equal to b, if yes then it returns 1 otherwise 0. This is printed in the console.
  • printf("\nLesser Than or Equal : %d",a<=b); uses the relational operator '<=' to check if the value of a is less than or equal to b, if yes then it returns 1 otherwise 0. This is printed in the console.
  • printf("\nEqual : %d",a==b); uses the relational operator '==' to check if the value of a is equal to b, if yes then it returns 1 otherwise 0. This is printed in the console.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

When you run this code, it will perform relational operations on variables and print the results in the console.


Source Code

//Relational Operators
 
#include<stdio.h>
int main()
{
    int a=10,b=5;
 
    printf("Greater Than            : %d",a>b);
    printf("\nLess Than             : %d",a<b);
    printf("\nGreater Than or Equal : %d",a>=b);
    printf("\nLesser  Than or Equal : %d",a<=b);
    printf("\nEqual                 : %d",a==b);
 
    return 0;
}
 
To download raw file Click Here

Output

Greater Than            : 1
Less Than             : 0
Greater Than or Equal : 1
Lesser  Than or Equal : 0
Equal                 : 0

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C