← Back to C Course | Chapter 12: Advanced Topics | Lesson 4 of 20

C Variable Length Arrays

A variable length array is an array whose size is decided while the program runs, like choosing how many seats to set out when you know the guest count. Its size is fixed once made.
Syntax
c
int size = value;
data_type array_name[size];  // size decided at runtime

What is a VLA?

A variable length array (VLA) lets you size an array using a variable's value at the moment the array is declared, instead of requiring a compile-time constant like int arr[10].

This is handy when you don't know an array's exact size until the program is already running, such as sizing a buffer to a user-provided count.

Example: What is a VLA?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 5
	int n = 5;
	int arr[n];
	arr[0] = 1;
	// Print formatted text to the screen
	printf("%d", arr[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Declaring and Using VLAs

To declare a VLA, first compute or read the size into an integer variable, then use that variable directly inside the array brackets, e.g. int n = getCount(); int values[n];.

The size expression is evaluated once at the point of declaration, so changing the variable afterward does not resize the array.

Example: Declaring and Using VLAs

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 3
	int n = 3;
	int values[n];
	// Loop: repeat while the condition holds
	for (int i = 0; i < n; i++) {
		values[i] = i * 2;
	}
	// Print formatted text to the screen
	printf("%d", values[2]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

VLAs inside Functions

VLAs are especially useful inside helper functions that need a temporary buffer sized exactly to the caller's input, avoiding the need to over-allocate a fixed maximum size or call malloc() for something short-lived.

The array automatically disappears when the function returns, with no manual cleanup required.

Example: VLAs inside Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printBuffer(int size) {
	int buffer[size];
	buffer[0] = 99;
	// Print formatted text to the screen
	printf("%d", buffer[0]);
}
// Program execution starts in main()
int main() {
	printBuffer(5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Memory Allocation of VLAs

Unlike malloc(), which reserves memory on the heap that persists until you explicitly free() it, a VLA is allocated on the stack and is automatically reclaimed the moment its enclosing scope ends.

This makes VLAs simpler to use for short-lived buffers, since there is no risk of forgetting to free them.

Example: Memory Allocation of VLAs

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void useVLA() {
	// Declare n and set it to 4
	int n = 4;
	int arr[n];
	arr[0] = 1;
	// Print formatted text to the screen
	printf("%d", arr[0]);
}
// Program execution starts in main()
int main() {
	useVLA();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Limits of VLAs

Stack space is much smaller than heap space (often just a few megabytes), so declaring a VLA sized by an unchecked or attacker-controlled variable can overflow the stack and crash the program.

Always validate that a VLA's requested size stays within a sane, expected bound before declaring it.

Example: Limits of VLAs

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 10
	int n = 10;
	int arr[n];
	arr[0] = 1;
	// Print formatted text to the screen
	printf("%d", arr[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Declaring a very large VLA, which can overflow the stack.
  2. Trying to initialize a VLA at declaration, which is not allowed.
  3. Assuming every compiler supports VLAs, when they became optional in C11.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.