Fundamentals 7 min read

for(;;) vs while(1): Which Infinite Loop Is Faster in Embedded C?

This article compares the infinite‑loop constructs for(;;) and while(1) in C, examining compiler optimizations, performance measurements on microcontrollers, coding style guidelines, and industry standards to determine if one form offers any real advantage over the other.

Open Source Linux
Open Source Linux
Open Source Linux
for(;;) vs while(1): Which Infinite Loop Is Faster in Embedded C?

Many engineers wonder whether to write an infinite loop in C as for(;;) or while(1). While some claim the two are equivalent after compiler optimization, others argue that for(;;) feels more like an unconditional jump and may be preferred by native English speakers.

In practice, modern compilers such as GCC and LLVM transform while(1) into an unconditional jump, so the generated machine code is essentially the same as for(;;). The difference is mostly psychological.

Some developers point out that certain legacy or specialized embedded compilers do not perform this optimization, causing while(1) to emit extra comparison instructions and a conditional jump, while for(;;) compiles to a single jmp. This can make while(1) slightly slower on such platforms.

Empirical tests were performed using MinGW, STM32F103, and ARMCC5 without optimization. The for(;;) version printed "for" in a tight loop, and the while(1) version printed "while". Assembly listings for both versions are shown below:

#include <stdio.h>
int main(){
    for(;;){
        printf("for
");
    }
}
Assembly for for(;;) version
Assembly for for(;;) version
#include <stdio.h>
int main(){
    while(1){
        printf("while
");
    }
}
Assembly for while(1) version
Assembly for while(1) version

Timing results showed that without optimization the for(;;) loop ran about 5.7% faster (45.86 ms vs 48.64 ms). With O3 optimization enabled, the difference vanished (both around 12.5 ms).

Style guidelines from standards such as GJB 8114‑2013, CppCoreGuidelines ES.73, and 360 Safe Rules recommend using while(1) for infinite loops because for is intended for loops with explicit iteration variables. In strict coding environments, while(1) is preferred for readability and maintainability.

In modern development with well‑optimized compilers, the two constructs are interchangeable; choose the one that makes the code clearer to you.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance TestingC programmingCompiler Optimizationembedded systemsinfinite loopfor vs while
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.