Subscribe Us

Relational Operator in C programming -smartduniya

Relational Operator in C

Introducing to C Relational Operators

C provides you a list of relational opeartors to allows you to compare data. The relational operators enable you to check whethere two variables or expressions are equal, not equal, which one is greater or less than other...etc. The relational operators are used in boolean conditions or expressions to return 0 or 1.The following table illustrates the relational operators in C:

Relational Operators Description
== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to

C Relational Operators Example

The following program demonstrates the relational operators in C:

#include <stdio.h>
#include <stdlib.h>
/**
* Relational operators demo
* @param x
* @param y
*/
void demo(int x, int y) {
printf("x = %d;y = %d\n", x, y);
if (x == y) {
printf("x is equal to y\n");
} else if (x != y) {
printf("x is not equal to y\n");
if (x > y) {
printf("x is greater than y\n");
} else {
printf("x is less than y\n");
}
}
};

/*
* main program
*/
int main(int argc, char** argv) {

int x = 10;
int y = 10;
demo(x, y);
x = 10;
y = 20;
demo(x, y);

x = 20;
y = 10;
demo(x, y);

return (0);
}

Here is the output of the demo program:

x = 10;y = 10
x is equal to y
x = 10;y = 20
x is not equal to y
x is less than y
x = 20;y = 10
x is not equal to y
x is greater than y

Post a Comment

0 Comments