If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C programming, comparison operators are used to compare the values of two operands and determine if a certain condition is true or false. Here are the most common comparison operators used in C:
==
: equal to. It returns true if the values of the operands are equal.Copy code
int x = 5, y = 3;
if (x == y) {
printf("x is equal to y");
} else {
printf("x is not equal to y");
}
This will output: "x is not equal to y"
!=
: not equal to. It returns true if the values of the operands are not equal.Copy code
int x = 5, y = 3;
if (x != y) {
printf("x is not equal to y");
} else {
printf("x is equal to y");
}
This will output: "x is not equal to y"
>
: greater than. It returns true if the value of the left operand is greater than the value of the right operand.Copy code
int x = 5, y = 3;
if (x > y) {
printf("x is greater than y");
} else {
printf("x is not greater than y");
}
This will output: "x is greater than y"
<
: less than. It returns true if the value of the left operand is less than the value of the right operand.Copy code
int x = 5, y = 3;
if (x < y) {
printf("x is less than y");
} else {
printf("x is not less than y");
}
This will output: "x is not less than y"
>=
: greater than or equal to. It returns true if the value of the left operand is greater than or equal to the value of the right operand.Copy code
int x = 5, y = 3;
if (x >= y) {
printf("x is greater than or equal to y");
} else {
printf("x is not greater than or equal to y");
}
This will output: "x is greater than or equal to y"
<=
: less than or equal to. It returns true if the value of the left operand is less than or equal to the value of the right operand.Copy code
int x = 5, y = 3;
if (x <= y) {
printf("x is less than or equal to y");
} else {
printf("x is not less than or equal to y");
}
This will output: "x is not less than or equal to y"
It's important to note that comparison operators return a Boolean value (true or false) and can be used in conditional statements (if, else, switch, etc.) and loops (while, for, etc.) to control the flow of the program based on the result of the comparison.
Comments: 0