What is the proper equivalent of whiletrue in plain C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In plain C, the common equivalent of while(true) is while (1) or for (;;) because any non-zero expression is treated as true. Both are valid and widely used for event loops, daemons, and embedded firmware tasks. The best choice depends on readability, standards, and how you signal loop termination.
Legal Infinite Loop Forms in C
C does not require a built-in true keyword unless you include boolean support from standard headers. Because loop conditions accept integer expressions, any non-zero constant can represent true.
Typical forms are:
while (1)for (;;)while (running)whererunningis controlled externally
Example:
This pattern is straightforward and easy to search in code reviews.
while (1) Versus for (;;)
Both compile to effectively the same control flow in modern compilers. The difference is mostly style.
while (1) emphasizes that the loop condition is always true.
for (;;) emphasizes that no initialization, condition, or increment is needed.
Pick one convention and keep it consistent across the codebase. Consistency improves maintainability more than micro-style preferences.
Using Boolean Types in Modern C
If you want a literal-style condition, include stdbool.h and use true explicitly.
This is valid in C99 and later. It may read better for teams that also work in C++ or other languages with explicit booleans.
Controlling Exit Conditions Cleanly
Most infinite loops are not truly endless. They wait for events until a shutdown signal, an error, or a completion condition occurs. Structure exits clearly to avoid hidden termination paths.
A robust pattern is to use a state flag and break on specific events.
This avoids abrupt termination and enables cleanup code after the loop.
Embedded and Systems Programming Considerations
In firmware and low-level services, infinite loops are normal because the process owns the device lifetime. Even then, include watchdog feeding, error handling, and sleep strategies to avoid busy spinning.
A naive loop can consume full CPU unnecessarily:
Prefer event wait or timed pause if possible:
That small pause can drastically reduce CPU waste in polling loops.
Readability and Static Analysis
Some static analyzers flag constant conditions unless configured. If your toolchain complains about while (1), use approved annotations or documented macros rather than obscure tricks.
For example, a project macro can communicate intent:
Use such macros sparingly. Over-abstraction for simple loops can reduce clarity.
Common Pitfalls
- Writing infinite loops without a clear shutdown path.
- Busy spinning without wait logic, causing high CPU usage.
- Mixing loop styles randomly, reducing codebase consistency.
- Hiding termination logic in deep nested branches.
- Assuming
trueexists without including proper boolean support.
Summary
- In plain C,
while (1)andfor (;;)are proper infinite-loop equivalents. - Both forms are valid and typically compile to similar machine code.
- Use
stdbool.hif explicittrueimproves readability. - Design loops with intentional exit and cleanup behavior.
- Avoid busy loops by adding event waits or timed sleeps when appropriate.

