0
0
CConceptBeginner · 3 min read

Variable Length Array in C: What It Is and How It Works

A variable length array (VLA) in C is an array whose size is determined at runtime, not at compile time. It allows you to create arrays with sizes based on variables, making your program more flexible when handling data of varying lengths.
⚙️

How It Works

A variable length array (VLA) in C works like a regular array but with a size that you decide while the program is running, not before it starts. Imagine you want to store a list of numbers, but you don't know how many until the user tells you. A VLA lets you create an array just big enough for that list.

When you declare a VLA, you use a variable to set its size. The computer then allocates the right amount of memory on the stack to hold the array. This is different from fixed-size arrays, where the size must be a constant number known when you write the code.

Think of it like ordering a pizza: with a fixed array, you order a pizza with a fixed number of slices every time. With a VLA, you decide how many slices you want only after you know how many people are eating.

💻

Example

This example shows how to declare and use a variable length array in C. The program asks the user for the number of elements, then creates an array of that size and fills it with values.

c
#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("Number of elements must be positive.\n");
        return 1;
    }

    int arr[n];  // Variable Length Array

    for (int i = 0; i < n; i++) {
        arr[i] = i * 2;
    }

    printf("Array elements:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
Output
Enter the number of elements: 5 Array elements: 0 2 4 6 8
🎯

When to Use

Use variable length arrays when you need to handle data whose size is not known until the program runs. For example, reading input from a user or processing files where the number of items varies.

VLAs are useful for temporary storage inside functions when you want to avoid fixed-size arrays that waste memory or are too small. However, be careful with very large sizes because VLAs use stack memory, which is limited.

If you need arrays that live longer or are very large, consider dynamic memory allocation instead.

Key Points

  • VLAs let you create arrays with sizes set at runtime.
  • They are allocated on the stack, so size should be reasonable.
  • VLAs improve flexibility compared to fixed-size arrays.
  • They are supported in C99 and later standards.
  • For very large or long-lived arrays, use dynamic memory allocation.

Key Takeaways

Variable length arrays allow array sizes to be set during program execution.
VLAs are allocated on the stack and should be used for moderate sizes.
They provide flexibility when handling data with unknown size at compile time.
VLAs are part of the C99 standard and later.
For large or persistent arrays, prefer dynamic memory allocation.