C relational operators
Created By: chatGPT
In C, relational operators are used to compare two values or expressions. The result of a relational operation is a boolean value: true or false. Here are the commonly used relational operators:
1. Equal to (==): This operator checks if two values are equal. For example, if you want to compare two integers, you can use this operator.
if (a == b) {
printf("a is equal to b");
}
2. Not equal to (!=): This operator checks if two values are not equal. It returns true if the values differ.
if (a != b) {
printf("a is not equal to b");
}
3. Greater than (>): This operator checks if the left operand is greater than the right operand.
if (a > b) {
printf("a is greater than b");
}
4. Less than (<): This operator checks if the left operand is less than the right operand.
if (a < b) {
printf("a is less than b");
}
5. Greater than or equal to (>=): This operator checks if the left operand is greater than or equal to the right operand.
if (a >= b) {
printf("a is greater than or equal to b");
}
6. Less than or equal to (<=): This operator checks if the left operand is less than or equal to the right operand.
if (a <= b) {
printf("a is less than or equal to b");
}
These operators can be used with various data types, including int, float, char, and double. When comparing strings, it's important to use functions like strcmp() from the string.h library since direct comparison won't work as expected.
Remember, a true condition is typically represented as 1 and a false condition as 0 in C. Using these operators correctly allows for effective control flow in programs.#include <string.h>
if (strcmp(str1, str2) == 0) {
printf("Strings are equal");
}