Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Logical OR operator (||)

The || operator returns int with value 1 when at least one operand is nonzero; otherwise it returns 0. The right operand may be skipped.

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

Syntax

left || right

left || right means logical OR. Its result has type int: 0 when both operands compare equal to zero, otherwise 1. Operands must have scalar types: arithmetic, pointer, or C23's nullptr_t.

The left operand is evaluated first. If nonzero, the right operand is not evaluated, including its side effects. Otherwise, a sequence point separates their evaluations.

Do not confuse || with bitwise |, which does not skip the right operand. In the example, 7 || ++calls skips the increment; 0 || ++calls performs it.

Examples

#include <stdio.h>

int main(void)
{
    int calls = 0;
    int first = 7 || ++calls;
    int second = 0 || ++calls;

    printf("%d %d %d\n", first, second, calls);
    return 0;
}

Result

1 1 1

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