Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

recursion — concept C23

Recursion calls a function on a smaller problem until a base case; no progress causes endless calls, and C guarantees neither tail-call elimination nor stack-exhaustion detection.

Version: C23

Syntax

recursion

Recursion calls a function on a smaller problem until a base case; no progress causes endless calls, and C guarantees neither tail-call elimination nor stack-exhaustion detection.

Recursion fits trees and divided problems when every call receives a strictly smaller range. State the base case before the recursive call and pass an accumulator when partial results need explicit control.

Test an empty structure, one element, and the maximum realistic depth. User-controlled data can exhaust the call stack; replace recursion with an explicit dynamic stack whose capacity and allocation failures can be checked.

Examples

#include <stdio.h>

static unsigned factorial(unsigned n) { return n < 2 ? 1u : n * factorial(n - 1); }
int main(void) { printf("%u\n", factorial(5)); return 0; }

Result

120

Recursion calls a function on a smaller problem until a base case; no progress causes endless calls, and C guarantees neither tail-call elimination nor stack-exhaustion detection.

Sources