In C, logical operators are used to perform logical operations, such as AND, OR, and NOT. The following are the logical operators in C:
- && (AND): The && operator is used to test if two conditions are both true. For example:
int a = 5;
int b = 2;
if (a > 0 && b < 10) { // this condition is true // code goes here }
- || (OR): The || operator is used to test if at least one of two conditions is true. For example:
int a = 5;
int b = 2;
if (a > 0 || b > 10) { // this condition is true // code goes here }
- ! (NOT): The ! operator is used to negate a condition. For example:
int a = 5;
int b = 2;
if (!(a > b)) { // this condition is false // code goes here }
It is important to use the correct logical operator based on the type of logical operation you want to perform. Using the wrong operator can result in errors or unexpected results in your code.