Fundamentals 6 min read

Do while(1) and for(;;) Really Differ? A Deep Dive into C Infinite Loops

This article explains the syntax and semantics of C's while(1) and for(;;) constructs, compares their execution flow, shows how to compile each to assembly with gcc, and demonstrates that, aside from file names, the generated code is identical under typical settings.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Do while(1) and for(;;) Really Differ? A Deep Dive into C Infinite Loops

Syntax of while(1)

The while loop repeats a block while a condition evaluates to true. The infinite‑loop form uses the constant 1 as the condition, which is always true.

while( expression ) {
    statements
}

Expression: the loop condition.

Statements: the loop body.

The condition is evaluated each iteration, so the compiler generates code that checks the constant 1 before jumping back.

Syntax of for(;;)

The for loop consists of three optional expressions: initialization, condition, and iteration. Using empty expressions creates an infinite loop because the condition defaults to true.

for( expression1 ; expression2 ; expression3 ) {
    statements
}
1. Evaluate expression1 (initialization). 2. Evaluate expression2 (condition). If true, execute the body; otherwise exit the loop. 3. After the body, evaluate expression3 (iteration). 4. Return to step 2. 5. When the loop ends, execution continues with the statement following the for construct.

Comparison and verification

Both forms implement an infinite loop, but developers often wonder whether the extra constant check in while(1) produces larger or slower code.

We compile two minimal programs with gcc -S to inspect the generated assembly.

// while.c
int main(int argc, char const *argv[]){
    while(1){}
    return 0;
}
// for.c
int main(int argc, char const *argv[]){
    for(;;){}
    return 0;
}

Assembly produced for while.c:

; filename: whiles
.file "while.c"
.text
.globl main
.type main, @function
main:
.LFB0:
    .cfi_startproc
    pushq %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq %rsp, %rbp
    .cfi_def_cfa_register 6
    movl %edi, -4(%rbp)
    movq %rsi, -16(%rbp)
.L2:
    jmp .L2
    .cfi_endproc
.LFE0:
    .size main, .-main
    .ident "GCC: (GNU) 9.3.0"
    .section .note.GNU-stack,"",@progbits

Assembly produced for for.c:

; filename: for.s
.file "for.c"
.text
.globl main
.type main, @function
main:
.LFB0:
    .cfi_startproc
    pushq %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq %rsp, %rbp
    .cfi_def_cfa_register 6
    movl %edi, -4(%rbp)
    movq %rsi, -16(%rbp)
.L2:
    jmp .L2
    .cfi_endproc
.LFE0:
    .size main, .-main
    .ident "GCC: (GNU) 9.3.0"
    .section .note.GNU-stack,"",@progbits

Aside from the comment indicating the source file name, the two outputs are identical, confirming that under the same compiler and optimization level the two infinite‑loop forms generate the same machine code.

Note that different compilers, source files, or optimization flags may produce variations, but conceptually while(1) and for(;;) are interchangeable for an infinite loop.

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.

CAssemblyprogramming fundamentalsgccwhile-loopinfinite loop
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.