Q:

Why is variable-length array working in this C code?

#include "stdio.h"

int main()
    {
        int n;
        printf("Enter n:\n");
        scanf("%d",&n);

        int arr[n][n];
        arr[3][3] = 4;
        printf("%d",arr[3][3]);
        getchar();
        return 0;
    }

Wasn't using int arr[n], where n is a variable, illegal in C? I am trying to understand what's happening here. Apparently the code works on my clang LLVM compiler and on IDEOne and on Codeblocks. I guess the compiler is just making things easy for me, by doing automatic memory allocation. But another astounding fact is that when I try to set n to 1, 2  or 3 it still works.

A:

Variable length arrays are allowed by C standard since C99. Note that they are still not allowed by C++
standard.
Also important point to note is for versions before c99 and for most C++ compilers variable length arrays are supported by implementations in the form of extensions.

