Compilation (gcc error)Compiler Syntax6 min read

Fix “variable-sized object may not be initialized” in C

Unlike C++, a `const int n = 5;` variable in C is NOT an integer constant expression. Writing `int arr[n] = {0};` defines a variable-length array (VLA), and ISO C forbids `= {0}` brace initializers on VLAs and static-duration declarations using non-constant sizes.

Quick Fix Summary

Use #define or enum { N = 5 }; for compile-time array sizes, or leave VLAs/heap allocations uninitialized in the declaration and zero them with memset or calloc.

Why This Error Happens

Why const int n = 5 triggers VLA rules in C

In C, const means read-only at runtime rather than a compile-time constant expression suitable for file-scope array sizes or {0} VLA zero-initialization.

Safer heap alternatives for runtime lengths

VLAs risk silent stack overflow if n grows large. Prefer calloc((size_t)n, sizeof(int)) for runtime lengths so zero-initialization and OOM NULL checking happen cleanly.

Top Root Causes & Specific Code Fixes

Initializing a variable-length array with = {0}

Standard C disallows brace initializers on arrays whose bound is not a constant expression.

Broken Pattern
int n = 5;
int scores[n] = {0};
Corrected Pattern
enum { N = 5 };
int scores[N] = {0};
Illustrative diagnostic (not executed):error: variable-sized object may not be initialized

Scalar vs aggregate char array invalid initializer

Initializing a char * with an uncast integer or struct with wrong field count fails type rules.

Broken Pattern
char buf[8] = 65;
Corrected Pattern
char buf[8] = "A";

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
#include <stdio.h>
int main(void) {
    const int n = 4;
    int arr[n] = {1, 2, 3, 4};
    printf("%d\n", arr[0]);
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
enum { ARR_LEN = 4 };
int main(void) {
    int arr[ARR_LEN] = {1, 2, 3, 4};
    printf("%d\n", arr[3]);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

4

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
variable-sized object may not be initializedBrace init `= {0}` on `int a[n]` where `n` is a variable/`const`-Wvla -WpedanticUse `enum { N = ... };` or `memset` / `calloc`
invalid initializer at file scopeInitializing static/global array with runtime var-std=c17Use compile-time integer constant expressions

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

Disallow VLAs Early
gcc -std=c17 -Wvla -Wall -Wextra main.c -o app

Highlights every variable-length array so you can replace runtime sizes with enum constants or calloc().

Strict Standard Mode
gcc -std=c17 -pedantic-errors main.c -o app

Enforces ISO C constant-expression rules cleanly.

Frequently Asked Questions

Does C23 support = {} empty initialization on VLAs?

C23 added empty `{}` zero-initialization for non-VLA objects and improved VLA zero-init (`= {}`), whereas fixed-size `enum`/`#define` or `calloc()` remain portable everywhere.

Why is const int SIZE = 10 invalid for global C arrays?

File-scope C variables require integer constant expressions; use `enum { SIZE = 10 };` or `#define SIZE 10`.

How should I zero a genuine runtime VLA?

Declare `int arr[n];` without `= {0}`, verify `n > 0`, and call `memset(arr, 0, sizeof arr);`.