CInfinite Loops

Infinite Loops

An infinite loop is a loop whose condition never becomes false, so it keeps repeating forever unless something inside it forces an exit. Sometimes this is a bug — other times it is exactly what the program is supposed to do, such as a server that must keep listening for connections until the process is shut down.

Writing an Intentional Infinite Loop
C has two common idioms for an intentional infinite loop: while (1) and for (;;). Both loop forever by construction — 1 is always true, and an empty for condition is treated as always true.

C
#include <stdio.h>

int main(void) {
    // Both of these loop forever on their own:
    // while (1) { ... }
    // for (;;) { ... }

    int count = 0;
    while (1) {
        printf("tick %d\n", count);
        count++;
        if (count >= 3) {
            break; // the loop's only way out
        }
    }

    return 0;
}
Tip
while (1) and for (;;) are functionally identical; some style guides prefer for (;;) because it avoids a compiler warning some tools emit about a constant condition, but either is completely standard C.
Real-World Use: Servers and Event Loops

Long-running programs such as network servers, embedded firmware, and GUI applications are typically structured around a central loop that runs for as long as the program is alive: wait for an event, handle it, then wait for the next one. This loop is not a bug — it is the entire point of the program, and it is expected to run indefinitely.

C
#include <stdio.h>

/* Simplified sketch of a server-style event loop. In real code,
   waitForEvent() would block on a socket, a hardware interrupt,
   or a message queue instead of returning canned values. */
int waitForEvent(void);   // returns an event code, or -1 to shut down
void handleEvent(int event);

int main(void) {
    for (;;) {
        int event = waitForEvent();

        if (event == -1) {
            printf("Shutdown signal received.\n");
            break; // the intended, controlled way out
        }

        handleEvent(event);
    }

    return 0;
}
Exiting an Intentional Infinite Loop
An infinite loop is only useful when there is a clear, intentional way to leave it. The most common exits are a break triggered by some internal condition, a return from inside the function containing the loop, or in long-running system processes, an external signal (such as SIGINT from Ctrl+C) handled elsewhere in the program.

C
#include <stdio.h>
#include <stdbool.h>

bool shouldStop(void); // pretend this checks some external condition

int main(void) {
    while (1) {
        printf("working...\n");

        if (shouldStop()) {
            return 0; // exits the loop AND the function in one step
        }
    }
}
Accidental Infinite Loops

Far more often, an infinite loop is an unintended bug: the programmer meant for the loop to end, but forgot to update the variable the condition depends on, or updated the wrong one.

C
#include <stdio.h>

int main(void) {
    int i = 0;

    // BUGGY: i is never modified inside the loop, so i < 5
    // is always true -- this prints forever.
    while (i < 5) {
        printf("i = %d\n", i);
    }

    return 0;
}
Warning
The bug above is one of the most common in all of C: a loop condition depends on a variable that the loop body never actually changes. Always double-check that every path through the loop body moves the loop measurably closer to its exit condition.

C
#include <stdio.h>

int main(void) {
    int i = 0;

    // FIXED: i is incremented every iteration, so i < 5
    // eventually becomes false.
    while (i < 5) {
        printf("i = %d\n", i);
        i++;
    }

    return 0;
}
Note
A closely related mistake is updating the wrong variable, or updating it inside a branch that doesn't always execute (for example, inside an if that is sometimes skipped). Trace through the loop body carefully and confirm the update genuinely runs on every single iteration.
Key Points
  • while (1) and for (;;) are the two standard idioms for writing an intentional infinite loop in C.

  • Servers, embedded firmware, and event-driven programs commonly use an infinite loop as their main structure by design.

  • An intentional infinite loop needs a clear exit: a break, a return, or an externally handled signal.

  • Accidental infinite loops usually come from forgetting to update the variable the condition depends on.

  • Always verify that the loop body actually moves every code path toward the exit condition.