QP School

Full Version: Infinite recursion in C
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Writing recursive functions without a proper base case can lead to infinite recursion.

// Incorrect
void countDown(int n) {
    printf("%d ", n);
    countDown(n - 1); // Missing base case
}

// Correct
void countDown(int n) {
    if (n <= 0) {
        return;
    }
    printf("%d ", n);
    countDown(n - 1);
}