Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The -- (decrement) operator is a unary arithmetic operator that decreases the value of its operand by one. It requires a modifiable lvalue of a scalar type (integer, floating-point, or pointer) as its operand. The operator exists in two forms, which differ strictly in their value computation relative to the side effect of modifying the operand.

Prefix Decrement (--x)

In the prefix form, the operator is placed before the operand.
  • Side effect: The value of the operand is decremented by 1.
  • Value computation: The expression evaluates to the new, decremented value of the operand.
int x = 10;
int y = --x; 
/* 
   1. x is decremented to 9.
   2. The expression '--x' evaluates to 9.
   3. y is assigned 9.
*/

Postfix Decrement (x--)

In the postfix form, the operator is placed after the operand.
  • Side effect: The value of the operand is decremented by 1.
  • Value computation: The expression evaluates to the original, un-decremented value of the operand.
int a = 10;
int b = a--; 
/* 
   1. The expression 'a--' evaluates to the original value, 10.
   2. a is decremented to 9.
   3. b is assigned 10.
*/

Technical Constraints and Behavior

Pointer Arithmetic When applied to a pointer, the -- operator does not simply subtract one byte from the memory address. Instead, it decrements the address by the size of the pointed-to type (sizeof(type)).
int *ptr = (int *)0x1004;
ptr--; // If sizeof(int) is 4, ptr becomes 0x1000, not 0x1003.
Rvalue Result In C, the result of both the prefix and postfix decrement operators is an rvalue (a temporary value), not an lvalue. Consequently, you cannot chain decrement operators or assign a value to the result of a decrement operation.
int x = 5;
--(--x); // Compilation error: lvalue required as decrement operand
(--x) = 10; // Compilation error: lvalue required as left operand of assignment
Sequence Points and Undefined Behavior The C standard dictates that the side effect of updating the operand’s value must be complete before the next sequence point. Modifying the same scalar object more than once between sequence points, or modifying it and reading its value for a purpose other than determining the value to be stored, invokes Undefined Behavior (UB).
int i = 5;
i = i--;       // Undefined Behavior: modifying 'i' twice without an intervening sequence point
int arr[5];
arr[i] = i--;  // Undefined Behavior: reading and modifying 'i' unsequenced
Master C with Deep Grasping Methodology!Learn More