The following table shows all the relational or comparison operators supported by the C language. Assume variable x holds 21, y holds 10 and variable z holds 0 then.
| Operator Name | Syntax | Description | Example | 
|---|---|---|---|
| Equal to | x == y | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | z = (x == y); z = 0  | 
| Not Equal to | x != y | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | z = (x != y); z = 1  | 
| Greater than | x > y | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | z = (x > y); z = 1  | 
| Less than | x < y | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | z = (x < y); z = 0  | 
| Greater than or equal to | x >= y | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | z = (x >= y); z = 1  | 
| Less than or equal to | x <= y | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | z = (x<= y); z = 0  | 
#include <stdio.h>
int main() 
{
 int x = 21;
 int y = 10;
 int z = 0;
 
 printf("Relational operators demo\n");
 printf("Value of x is %d\n", x );
     printf("Value of y is %d\n", y );
 printf("Value of z is %d\n", z );
 
 z = (x == y);
 printf("Equal to (z = (x == y)): %d\n",z);
 
 z = (x != y);
 printf("Not equal to (z = (x != y)): %d\n",z);
 
 z = (x > y);
 printf("Greater than (z = (x > y)): %d\n",z);
 
 z = (x < y);
 printf("Less than (z = (x < y)): %d\n",z);
 z = (x >= y);
 printf("Greater than or equal to (z = (x >= y)): %d\n",z);
 z = (x <= y);
 printf("Less than or eqaul to (z = (x <= y)): %d\n",z);
    return 0;
}
The output of the above program would be:
Relational operators demo
Value of x is 21
Value of y is 10
Value of z is 0
Equal to (z = (x == y)): 0
Not equal to (z = (x != y)): 1
Greater than (z = (x > y)): 1
Less than (z = (x < y)): 0
Greater than or equal to (z = (x >= y)): 1
Less than or eqaul to (z = (x <= y)): 0
Related topics:
Overview of Operators in C | Arithmetic Operators in C | Logical Operators in C | Bitwise Operators in C | Compound Assignment Operators in C | Conditional Operators in C | Miscellaneous Operators in C | Operator Precedence and Associativity in C
List of topics: C Programming
No comments:
Post a Comment