Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Logical negation operator !

The ! operator reverses truth: it produces 1 for zero and 0 for a nonzero value. Its result has type int.

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

Syntax

!expression

The unary operator ! accepts a scalar operand of arithmetic or pointer type or, in C23, of type nullptr_t. The expression !E is equivalent to 0 == E: it returns 1 when the operand equals zero, and 0 otherwise. The result has type int, not bool. In C23, !nullptr yields 1 of type int.

For a pointer, !p tests whether it is a null pointer; it does not read the pointed-to object. Double negation !!x normalizes a value to 0 or 1, including negative numbers. Do not confuse ! with ~, which inverts the bits of an integer operand.

Examples

#include <stdio.h>

int main(void)
{
    int x = -7;
    int *p = NULL;

    printf("%d %d %d %d\n", !0, !x, !!x, !p);
    return 0;
}

Result

1 0 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