Menu Close

C Programming: Logical Operators

Posted in C Programming

Logical operators in C are used to perform logical operations on boolean values (true or false). They evaluate the conditions and return a boolean result. C language supports three logical operators: && (logical AND), || (logical OR), and ! (logical NOT).

1. Logical AND (&&)

Description: The logical AND operator returns true if both of its operands are true. Otherwise, it returns false.

Example:

int a = 5;
int b = 10;
int c = 0;
int result1, result2;

result1 = (a > 0 && b > 0);  // Evaluates to true (1) because both 'a' and 'b' are positive
result2 = (a > 0 && c > 0);  // Evaluates to false (0) because 'c' is not positive

printf("Result1: %d\n", result1);  // Output: Result1: 1
printf("Result2: %d\n", result2);  // Output: Result2: 0

2. Logical OR (||)

Description: The logical OR operator returns true if at least one of its operands is true. If both operands are false, it returns false.

Example:

int a = 5;
int b = 10;
int c = 0;
int result1, result2;

result1 = (a > 0 || b > 0);  // Evaluates to true (1) because at least one of 'a' and 'b' is positive
result2 = (a > 0 || c > 0);  // Evaluates to true (1) because 'a' is positive

printf("Result1: %d\n", result1);  // Output: Result1: 1
printf("Result2: %d\n", result2);  // Output: Result2: 1

3. Logical NOT (!):

Description: The logical NOT operator reverses the logical state of its operand. If the operand is true, it returns false. If the operand is false, it returns true.

int a = 5;
int b = 0;
int result1, result2;

result1 = !b;              // Evaluates to true (1) because 'b' is zero
result2 = !(a > 10);       // Evaluates to true (1) because 'a' is not greater than 10

printf("Result1: %d\n", result1);  // Output: Result1: 1
printf("Result2: %d\n", result2);  // Output: Result2: 1

In the examples provided, the results of the logical operations are assigned to variables result1 and result2 for clarity. The values of these variables are then printed using printf statements. The results demonstrate the logical behavior of the operators where true is represented by 1 and false is represented by 0.

Related:   Basic Logical Operations : AND, OR, NOT Logic Gates
Operator Description Example
(a and b, a = 1, b = 0)
&& logical AND operator. It returns true if both operands are non-zero a && b, result: false
|| logical OR operator. It returns true if either of the operands are non-zero a || b, result: true
! logical NOT operator. is used to invert the logical state of an operand !a, result:0

4. Examples

Example 4.1) Logical Operators

#include <stdio.h>

int main() {

   int a = 1, b = 0, result;

   // And
   result = (a && b);
   printf("a && b = %d \n",result);

   // Or
   result = (a || b);
   printf("a || b = %d \n",result);

   // Not
   result = !a;
   printf("!a = %d \n",result);

   return 0;

}

Results:

a && b = 0
a || b = 1
!a = 0

Process returned 0 (0x0) execution time : 0.032 s
Press any key to continue.

Example 4.2 ) Logical Operators

 

// Working of logical operators

#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10, result;

    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) is %d \n", result);

    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) is %d \n", result);

    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) is %d \n", result);

    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) is %d \n", result);

    result = !(a != b);
    printf("!(a != b) is %d \n", result);

    result = !(a == b);
    printf("!(a == b) is %d \n", result);

    return 0;
}

Results:

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c< b) is 0
!(a != b) is 1
!(a == b) is 0

5. Short-Circuit Evaluation

The && (logical AND) and || (logical OR) are logical operators in the C language, and they follow the principle of short-circuit evaluation. This means that when evaluating these operators, if the value of the left operand is sufficient to determine the overall result of the expression, the right operand will not be evaluated.

Related:   What is the Difference Between Integer Data Type and Floating Point Data Type ?

Short-Circuit AND (&&): If the left operand is false, the entire expression will be false, and the right operand will not be evaluated, as the overall result is already determined to be false.

if (x > 0 && y > 0) {
    // Execute this code only if both x and y are greater than 0
}

Short-Circuit OR (||): If the left operand is true, the entire expression will be true, and the right operand will not be evaluated, as the overall result is already determined to be true.

if (x == 0 || y == 0) {
    // Execute this code if either x or y is equal to 0
}

This short-circuit behavior is useful in certain situations, helping to improve program performance and prevent unnecessary computations.

Example 5.1)  short-circuit evaluation

#include <stdio.h>;
#include <stdlib.h>
#include <string.h>
 
int main()
{
    int i = 0, j = 1;
 
    if (i && i++) {
        ; 
    }
    printf("%d\n", i); // The value of i has not increased, indicating that i++ has not been computed.
 
    if (j || j++) {
        ;
    }
    printf("%d\n", j); // The value of j has not increased, indicating that j++ has not been computed.
    return 0;
}

Results:

0
1

 

Leave a Reply