• Home
  • 3.10 Logical AND

3.10 Logical AND

View Categories

3.10 Logical AND

< 1 min read

Table of Contents

The logical AND operator && combines two conditions. The result is 1 only when both operands are non-zero; otherwise, the result is 0.

#include <stdio.h>

int main()
{
    int age = 25;
    int has_id = 1;

    printf("Result: %d\n", age >= 18 && has_id);

    return 0;
}

Example Output #

Result: 1

Explanation #

The expression:

age >= 18 && has_id

contains two operands:

age >= 18
has_id

age >= 18 produces 1 because age is 25. has_id also has the non-zero value 1.

Since both operands are non-zero, the && expression produces 1.

The logical AND operator follows these results:

Operand 1 Operand 2 Result
0 0 0
0 non-zero 0
non-zero 0 0
non-zero non-zero 1

C also uses short-circuit evaluation for &&. If the left operand evaluates to 0, the right operand is not evaluated because the overall result is already known to be false.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *