Question:
Despite the notion that variable length arrays (VLAs) are not a part of the C standard, why does the following code compile and execute successfully:
#include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i=0; i<n; i++) { a[i] = i; } for (int i=0; i<n; i++) { cout << a[i] << endl; } }
Answer:
The C standard does not mandate compilers to support VLAs. However, compiler vendors may include VLAs as an extension. For instance, GCC versions 4.7 and later do support VLAs.
VLAs were initially proposed for inclusion in C 14 but were not accepted. They were also not included in subsequent C 17 revisions.
The above is the detailed content of Why Do My Variable Length Arrays Compile and Execute in C Despite the Standard?. For more information, please follow other related articles on the PHP Chinese website!