Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Logical AND operator (&&)

The && operator tests whether both operands are nonzero. It returns int 0 or 1 and skips the right operand when the left is zero.

Version: C23; repository compiler flag, production runtime not attested

Syntax

left && right

left && right yields 1 when both operands compare unequal to zero; otherwise it yields 0. The result has type int, not bool. Operands must have scalar type, such as arithmetic or pointer type.

The left operand is evaluated first. If it is zero, the right operand is not evaluated: this is short-circuit evaluation. Otherwise, a sequence point separates their evaluations: the left operand's side effects finish before the right operand.

Do not confuse && with bitwise &, which provides no such skipping. In the example, zero divisor causes both ++checks and the division to be skipped. Reversing the operands would remove this protection.

Examples

#include <stdio.h>

int main(void)
{
    int divisor = 0;
    int checks = 0;
    int result = (divisor != 0) && (++checks, 12 / divisor > 2);

    printf("result=%d checks=%d\n", result, checks);
    return 0;
}

Result

result=0 checks=0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources