Home  >  Article  >  Backend Development  >  Do Variable Length Arrays (VLAs) Exist in C ?

Do Variable Length Arrays (VLAs) Exist in C ?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-16 13:11:03728browse

Do Variable Length Arrays (VLAs) Exist in C  ?

Variable Length Arrays in C

Despite the common assumption that C does not support variable length arrays (VLAs), there are ways to achieve similar functionality. However, it's important to note that using these methods is not guaranteed compatibility across all compilers.

Code Explanation

The example code you provided compiles and works because:

  • GCC Compatibility: GCC version 4.7 and later support VLAs as an extension. This means that when your code is compiled with GCC, it interprets int a[n]; as a VLA.
  • Automatic Storage Allocation: VLAs are stored on the stack, which is a type of memory allocated automatically at the start of a function. So, in your code, a is allocated on the stack based on the value of n entered by the user.

Standard Support

It's important to note that the C standard does not require compilers to support VLAs. Therefore, code that uses VLAs may not be portable across different compilers or platforms.

Failed Proposal for C 14 and C 17

It was initially proposed that VLAs would be included in the C 14 standard. However, the proposal did not pass the voting process, and VLAs were not added to the language. The proposal also failed to be accepted for inclusion in C 17.

Conclusion

While VLAs are not officially part of the C standard, they can be used as an extension in certain compilers, such as GCC. However, it's crucial to be aware of potential portability issues and use VLAs with caution, especially in cross-platform code.

The above is the detailed content of Do Variable Length Arrays (VLAs) Exist in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn