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
while (1) and for (;;). Both loop forever by construction — 1 is always true, and an empty for condition is treated as always true.#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;
}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.
#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
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.#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.
#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;
}#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;
}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.