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
- Pass -std=c99 (or c11) to the C compiler.
- 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
gcc "error: 'nullptr' was not declared" (missing -std=c++11) in CIFix gcc/clang "error: 'nullptr' was not declared in this scope" in CI - a C++11 feature is used while the com…
gcc "error: implicit declaration of function" (C) in CIFix gcc "error: implicit declaration of function" in C builds in CI - a function is called without a prior pr…
g++ ABI Mismatch - _GLIBCXX_USE_CXX11_ABI Undefined ReferencesFix C++ ABI mismatch in CI - undefined references to std::__cxx11 symbols when objects/libraries are built wi…