Hello everyone, I am Xiaokang.
Today, let’s talk about a severely misunderstood feature of the C language—Variable Length Arrays!
Have you heard the saying, “Variable length arrays cannot be defined in C language”?
If you believed that, you have been seriously misled! Let’s uncover this widely spread “lie” and get to the bottom of it!
1. Hold on, what is a Variable Length Array?
In simple terms, a Variable Length Array (VLA) is an array whose size is not fixed and is determined at runtime.
Traditional C language arrays are defined like this:
int arr[10]; // Fixed size integer array of 10
This type of array has its size determined at compile time and cannot be changed once defined. But a variable length array is defined like this:
int n = 5; // n can be a variable
int arr[n]; // Array size is determined by variable n
Looks simple, right? But many people think that C language does not support this syntax!
2. Where did the “lie” come from?
This misunderstanding mainly arises from:
- The early C standards (C89/C90) indeed did not support variable length arrays
- Some compilers may not fully support them
- People repeating what they hear, instructors or textbooks explaining C language may be outdated
Thus, the saying “C language cannot define variable length arrays” spread in the programming community…
3. The truth: C99 standard has supported variable length arrays for a long time!
That’s right! Since the C99 standard in 1999, the C language has officially supported variable length arrays! It’s been over 20 years, how come your instructors haven’t updated their knowledge base? 😂
Let’s look at a simple example:
#include <stdio.h>
int main() {
printf("Please enter the size of the array:");
int size;
scanf("%d", &size);
// This is a variable length array! Size is determined by user input
int numbers[size];
printf("Please enter %d integers:\n", size);
for(int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
// Calculate average
int sum = 0;
for(int i = 0; i < size; i++) {
sum += numbers[i];
}
printf("The average is: %.2f\n", (float)sum / size);
return 0;
}
This code complies with the C99 standard, but remember, not all compilers support the feature of variable length arrays! For example, the MSVC (Microsoft Visual C++) that comes with Visual Studio does not support variable length arrays.
4. What are the considerations for variable length arrays?
Although variable length arrays are convenient, there are some limitations and considerations:
1. Can only be defined inside functions: Variable length arrays cannot be global or static variables
// Incorrect usage
int n = 10;
int global_arr[n]; // Compilation error!
// Correct usage
void func(int n) {
int local_arr[n]; // This is allowed
}
2. Cannot be initialized: Cannot directly assign initial values when defining
int n = 5;
int arr[n] = {1, 2, 3, 4, 5}; // Compilation error!
3. Memory allocated on the stack: Variable length arrays allocate memory on the stack, and if the array is too large, it may cause stack overflow
int n = 1000000; // A very large number
int huge_arr[n]; // Dangerous! May cause stack overflow
4. Compatibility issues: The C++ standard does not support variable length arrays (although some C++ compilers support them as an extension)
5. How frequently are they used in practical applications?
To be honest, variable length arrays are not used particularly frequently in actual projects, mainly due to:
- Memory safety considerations: Since memory is allocated on the stack, arrays of uncontrolled size may lead to stack overflow
- Compatibility issues: Some embedded systems or older compilers may not support the C99 standard
- Alternatives for dynamic memory allocation: For truly dynamically sized arrays, many programmers prefer to use
<span>malloc</span>/<span>free</span>
// Alternative using malloc
int *arr = (int *)malloc(size * sizeof(int));
if(arr != NULL) {
// Use arr
free(arr); // Remember to free memory after use
}
However, variable length arrays are still very useful in the following scenarios:
- Simple short-lived functions: When the array size is moderate and used only within the function
- Teaching and learning: Understanding stack memory allocation mechanisms
- Algorithm implementation: Some algorithms that require temporary arrays
6. How to choose? Variable Length Arrays vs Dynamic Memory Allocation
So, when should you use variable length arrays, and when should you use malloc? Here’s a simple guide:
- Use variable length arrays: When the array size is moderate (within a few MB) and used only briefly within the current function
- Use malloc: When the array is large, needs to exist for a long time, or needs to be passed between functions
Conclusion: Don’t be fooled by the “lie” anymore!
The C language absolutely supports variable length arrays, which is an official feature of the C99 standard! Next time someone tells you it can’t be done, just retort, “Dude, your knowledge needs an update!”
Should I use variable length arrays or not?
It’s just like whether to eat spicy food—depends on the situation! 😄
For simple scenarios, you can use: Temporary small arrays? Variable length arrays are quick and convenient, and the code is clean.
For large projects, don’t use them: They can easily lead to stack overflow, have poor compatibility, and are difficult to debug. Some large companies even have coding standards that prohibit them.
In short: Understand it, use it moderately, and don’t misuse it. It’s like a “double-edged sword” in martial arts novels; it can be cool to use, but you have to be careful!
If you have any questions about the C language, feel free to leave a comment. See you next time!

END
Author:xiaokang1998
Source:Learn Programming with XiaokangCopyright belongs to the original author. If there is any infringement, please contact for deletion..▍Recommended ReadingOverview of job positions and salary status at Zhihui Jun CompanyA guide to writing embedded driver programs (based on timing diagrams)Why is hardware harder than software, yet hardware engineers earn less than software engineers?→ Follow for more updates ←