Skip to content
Latchkey

gcc "error: 'for' loop initial declarations are only allowed in C99"

Declaring the loop counter inside the for statement is a C99 feature. A C compiler defaulting to C89/C90 rejects it.

What this error means

A C file fails on for (int i = 0; ...) telling you initial declarations are only allowed in C99 or C11 mode, only on an older default standard.

gcc
loop.c:4:3: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
    4 |   for (int i = 0; i < n; i++)
      |   ^~~

Common causes

How to fix it

Compile in C99 or later

  1. Pass -std=c99 (or c11) to the C compiler.
  2. In CMake set CMAKE_C_STANDARD instead of a hardcoded flag.
gcc
gcc -std=c99 loop.c -o app
# CMake
set(CMAKE_C_STANDARD 99)

How to prevent it

  • Pin the C standard with -std= or CMAKE_C_STANDARD so the build does not depend on a default that varies by compiler.

Frequently asked questions

What causes ""for loop initial declarations""?
A C file fails on for (int i = 0; ...) telling you initial declarations are only allowed in C99 or C11 mode, only on an older default standard.
How do I fix "for loop initial declarations"?
Compile in C99 or later

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card