Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite recursion in C
#1
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);
}
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Forum Jump:


Users browsing this thread: 6 Guest(s)