Fundamentals 5 min read

Differences Between while(1) and for(;;) Infinite Loops in C

This article explains the syntax, execution flow, and practical differences between the C constructs while(1) and for(;;), demonstrating through compiled assembly that both produce identical code for an empty infinite loop under typical compiler settings.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Differences Between while(1) and for(;;) Infinite Loops in C

The article addresses a common question about whether while(1) and for(;;) are equivalent infinite loops in C, describing their syntax, execution flow, and semantic differences.

It first presents the syntax of each construct, showing the while form as while (expression) { statements } and the for form as for (expr1; expr2; expr3) { statements } , and explains how the condition is evaluated.

For while(1) , the constant expression 1 is evaluated each iteration, while for for(;;) the three empty expressions are typically optimized away, resulting in a direct infinite jump.

To verify practical differences, the article provides two source files and shows the GCC commands used to generate assembly:

gcc -S -o while.s while.c
gcc -S -o for.s for.c

The source code of the two files is shown below:

// filename: while.c
int main(int argc, char const *argv[])
{
    while(1)
    {}

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

    return 0;
}

The resulting assembly listings are displayed, revealing that aside from the source filename comment, the generated code is identical, confirming that both constructs compile to the same machine code under typical settings.

; 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
; 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

The article notes that different compilers, optimization levels, or more complex loop bodies may produce variations, but for the simple empty-loop case the two forms are interchangeable.

compilerassemblyC programmingloopsforwhile
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.