login

C Relational Operators

Relational operators in C programming language are as follows: <, <=, >, >=, ==, != . They are used in Boolean conditions or expression and returns true or false. Here is a program which demonstrate relational operators:
#include <stdio.h>
/* a program demonstrates C relational operators */
 
 
void print_bool(bool value){
    value == true ? printf("true\n") : printf("false\n");
};
 
void main(){
    int x = 10, y = 20;
    
    printf("x = %d\n",x);
    printf("y = %d\n",y);
    
    /* demonstrate == operator */
    bool result = (x == y);
    printf("bool result = (x == y);");
    print_bool(result);
 
    /* demonstrate != operator */
    result = (x != y);
    printf("bool result = (x != y);");
    print_bool(result);
 
    /* demonstrate > operator */
    result = (x > y);
    printf("bool result = (x > y);");
    print_bool(result);
 
    /* demonstrate >= operator */
    result = (x >= y);
    printf("bool result = (x >= y);");
    print_bool(result);
 
    /* demonstrate < operator */
    result = (x < y);
    printf("bool result = (x < y);");
    print_bool(result);
 
    /* demonstrate <= operator */
    result = (x <= y);
    printf("bool result = (x <= y);");
    print_bool(result);
    
 
    /* keep console screen until a key stroke */
    char key;
    scanf(&key);
}

Here is the output:

x = 10
y = 20
bool result = (x == y);false
bool result = (x != y);true
bool result = (x > y);false
bool result = (x >= y);false
bool result = (x < y);true
bool result = (x <= y);true