C Variable Length Arrays

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Variable Length Arrays?

A variable length array (VLA) has a size computed at run time: int a[n];. It lives on the stack and disappears at the end of its block. VLAs were required in C99, are optional in C11, and cannot be initialized in the declaration. Large sizes risk stack overflow.

Remember
int n = 4;
int sq[n];             /* size decided at run time */
sizeof sq;             /* evaluated at run time    */

Cheat Sheet

Keep these facts handy while you work with Variable Length Arrays in C.

TopicDetail
StandardC99 mandatory, C11 optional
StorageStack, automatic duration
InitializerNot allowed
Alternativemalloc for big or long-lived data

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Arrays and Functions.

Example 1 — A VLA sized by a variable

Fill with squares and report the run-time size.

C
#include <stdio.h>

int main(void) {
    int n = 4;
    int sq[n];

    for (int i = 0; i < n; i++) {
        sq[i] = i * i;
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", sq[i]);
    }
    printf("\nsizeof sq = %zu\n", sizeof sq);
    return 0;
}

How It Works

  1. n is a variable, so sq is a VLA.
  2. There is no initializer list; elements are filled by the loop.
  3. sizeof sq is computed at run time: 4 ints * 4 bytes = 16.

Edge Cases & Pitfalls

Check these before relying on Variable Length Arrays in real programs.

size

Unvalidated size

A negative or huge n is undefined or overflows the stack. Validate before declaring.

msvc

Not supported everywhere

MSVC does not support VLAs. Use malloc for portable code.

Key Takeaways

  • Runtime size: only for automatic arrays.
  • Risk: stack usage.
  • Portable: prefer malloc for large sizes.

One line: A variable length array (VLA) has a size computed at run time: int a[n];.

Frequently Asked Questions

A VLA is an array whose length is determined at run time, declared with a non-constant size like int a[n];.
Only for small, validated sizes. Large VLAs can overflow the stack, so many projects ban them in favor of malloc.
Continue with "C Arrays and Functions" next. Pass arrays and 2D arrays to functions, and fill arrays from functions. The C Arrays hub lists every topic in order.

Next: Arrays and Functions

Pass arrays and 2D arrays to functions, and fill arrays from functions.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful