QP School
Infinite recursion in C - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: C 18 Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=32)
+--- Thread: Infinite recursion in C (/showthread.php?tid=5123)



Infinite recursion in C - Qomplainerz - 07-26-2023

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);
}