Infinite Loops in C: Types of Infinite Loops
What is an Infinite Loop?
An infinite loop is a loop whose terminating condition never becomes false, so it keeps executing forever unless explicitly broken out of.
Examples
// Using for
for (;;) {
printf("Running\n");
}
// Using while
while (1) {
printf("Running\n");
}
// Using do-while
do {
printf("Running\n");
} while (1);
When Are They Useful?
- Server or event-listening programs that must run continuously.
- Menu-driven programs that loop until the user chooses to exit, using
break.
Accidental infinite loops are a common bug — always ensure there's a valid exit condition or break statement.
How an Exit Condition Actually Ends a Loop
A loop keeps running while its condition is true. Writing while (1) or for (;;) makes the condition permanently true, so nothing inside the loop can end it by the condition alone — something must actively break out. The usual fix is to change a value that the condition reads, or to use break. If neither happens, the loop runs forever and the program hangs.
int count = 0;
while (1) {
printf("%d\n", count);
count++;
if (count == 5) break; // the exit the loop needs
}
Breaking Out Safely With break and Flags
Intentional infinite loops power menu programs and event listeners. The pattern is to loop forever on purpose and leave only on a clear signal — a menu choice, an end-of-input, or an error:
int choice;
while (1) {
printf("1=run 0=exit: ");
if (scanf("%d", &choice) != 1) break; // bad input ends it too
if (choice == 0) break; // user chose to quit
printf("running...\n");
}
Checking the return value of scanf matters: if input fails and you do not handle it, the loop can spin forever reading the same bad data.
Spotting an Accidental Infinite Loop
- The counter never reaches the condition. Incrementing by 2 while testing
i == 5from an odd start skips the target forever — test with<or<=instead of==. - The update is inside an
ifthat rarely runs, so most iterations change nothing. - A floating-point counter compared for exact equality, which rounding may never satisfy.
- A missing
breakin a loop that was meant to exit on a condition.
When a program freezes and burns CPU, suspect a loop whose exit is never reached. Print the controlling variable inside the loop to see whether it is moving toward the condition at all.
Two C-specific traps deserve a mention. A stray semicolon right after the header, as in while (i < 10);, gives the loop an empty body, so the real block runs once afterward and i never advances. And using assignment = where you meant comparison ==, as in while (x = 5), sets x to 5 every time and tests a value that is always true. Both compile cleanly, so the compiler will not warn you unless you turn warnings up.
Takeaway: an infinite loop is only a bug when it is unintentional. Deliberate ones are fine as long as there is a reliable break; accidental ones nearly always come down to an update that never pushes the condition toward false.
Ready to master real-world C Programming development?
Learn C Programming hands-on with mentor-led, live sessions.