If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C programming, logical operators are used to perform logical operations on variables and constants. Here are the most common logical operators used in C:
&&
: logical AND operator. It returns true if both the operands are true, otherwise it returns false.Copy code
int x = 5, y = 3;
if (x > 4 && y > 2) {
printf("x is greater than 4 and y is greater than 2");
} else {
printf("x is not greater than 4 or y is not greater than 2");
}
This will output: "x is greater than 4 and y is greater than 2"
||
: logical OR operator. It returns true if either of the operands is true, otherwise it returns false.Copy code
int x = 5, y = 3;
if (x > 4 || y > 4) {
printf("x is greater than 4 or y is greater than 4");
} else {
printf("x is not greater than 4 and y is not greater than 4");
}
This will output: "x is greater than 4 or y is greater than 4"
!
: logical NOT operator. It returns true if the operand is false and false if the operand is true.Copy code
int x = 5;
if (!(x > 4)) {
printf("x is not greater than 4");
} else {
printf("x is greater than 4");
}
This will output: "x is
Comments: 0