Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Decrement operator --

The `--` operator performs a decrement. In C23, decrementing a `bool` that holds `false` stores `true`. The prefix form yields the new value; the postfix form yields the previous value.

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

Syntax

--operand
operand--

--x updates x and yields the new value; x-- yields the previous value and also updates x. As separate statements, --x; and x--; have the same effect.

The operand must be a modifiable lvalue of real or pointer type; const objects and literals do not qualify. A pointer moves back one element, not one byte, within permitted pointer-arithmetic bounds.

For a real operand, the update subtracts 1 and converts the result back to the operand’s type. In C23, integer promotion converts a bool value of false to 0 of type int. Subtracting 1 gives -1, which converts to true when stored as bool.

Avoid unsequenced modifications and reads, such as x-- + x: behavior is undefined. Signed int overflow is also undefined; unsigned int wraps modulo its range.

In the example, before receives 5, then x becomes 4; after and final x are 3.

Examples

#include <stdio.h>

int main(void)
{
    int x = 5;
    int before = x--;
    int after = --x;

    printf("before=%d after=%d x=%d\n", before, after, x);
    return 0;
}

Result

before=5 after=3 x=3

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