Home > Backend Development > C++ > Why Do My Variable Length Arrays Compile and Execute in C Despite the Standard?

Why Do My Variable Length Arrays Compile and Execute in C Despite the Standard?

Barbara Streisand
Release: 2024-11-12 13:35:02
Original
446 people have browsed it

Why Do My Variable Length Arrays Compile and Execute in C   Despite the Standard?

Are Variable Length Arrays Supported in C ?

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;
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template