Compiler WarningCompiler Syntax5 min read

Fix “ISO C90 forbids mixed declarations and code” in C

Original ANSI C89 / ISO C90 required every local variable inside `{ ... }` to be declared before the first executable statement of that block and forbade `for (int i = 0; ...)`. C99, C11, C17, and C23 removed both restrictions, but legacy Makefiles using `-ansi` or `-std=c90 -pedantic-errors` still enforce C90 rules.

Quick Fix Summary

Compile modern code with -std=c17 (or -std=c11). When targeting a strict C89/C90 embedded or kernel codebase that enforces -Wdeclaration-after-statement, declare all block variables at the top of { ... } before calling any functions.

Why This Error Happens

Why old Makefiles and grading scripts trigger this error

University autograders and older embedded SDKs occasionally compile with gcc -ansi -pedantic-errors (-ansi is an alias for -std=c90), rejecting for (int i = 0; ...) and mid-function declarations.

Kernel coding styles (-Wdeclaration-after-statement)

Even under -std=gnu11, projects that explicitly pass -Wdeclaration-after-statement -Werror intentionally enforce top-of-block declarations.

Top Root Causes & Specific Code Fixes

Declaring a variable after printf() or an assertion under -std=c90

C90 requires all declarations in a compound block to precede executable statements.

Broken Pattern
printf("Starting...\n");
int count = 5;
Corrected Pattern
int count = 5;
printf("Starting...\n");
Illustrative diagnostic (not executed):error: ISO C90 forbids mixed declarations and code

Writing for (int i = 0; i < n; i++) with -ansi or GCC 4.x defaults

`for`-init declarations were introduced in ISO C99.

Broken Pattern
/* compiled with gcc -std=c90 -pedantic-errors */
for (int i = 0; i < 3; i++) { printf("%d ", i); }
Corrected Pattern
int i;
for (i = 0; i < 3; i++) { printf("%d ", i); }

Complete Broken and Corrected Programs

Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.

Read the broken program
/* Fails when compiled with: gcc -std=c90 -pedantic-errors */
#include <stdio.h>
int main(void) {
    puts("begin");
    int x = 42;
    printf("%d\n", x);
    return 0;
}
Open broken program in compiler
Read the corrected program
/* Portable across C89/C90, C99, C11, and C17 */
#include <stdio.h>
int main(void) {
    int x = 42;
    puts("begin");
    printf("%d\n", x);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

begin
42

Debugging Techniques and Commands

Select Modern C17 Mode
gcc -std=c17 -Wall -Wextra main.c -o app

Enables C99/C11/C17 mixed declarations and `for (int i = 0; ...)` loop variables.

Test Strict C90 Portability Locally
gcc -std=c90 -pedantic-errors main.c -o app

Verifies whether code satisfies legacy C89/C90 autograders before submission.

Frequently Asked Questions

What does gcc -ansi do?

`-ansi` in C mode selects strict ISO C90 (`-std=c90`), disabling C99 `//` comments, `for (int i = ...)`, and declarations after statements.

Which GCC version switched the default C standard away from gnu89?

GCC 5 switched the default from `gnu89` to `gnu11`, and GCC 8+ / GCC 15 moved to `gnu17` / `gnu23`.

Can I open an inner { ... } block in C90 to declare a new variable mid-function?

Yes. Every `{ ... }` compound statement starts a fresh block scope in C90 where declarations are allowed at the top of that inner block.