Search code examples
carraysmemory-managementstatic-memory-allocation

Array size should be known at compile time


I wrote a simple code where I am creating array without a fixed size. I tried compiling the code in gcc and it is working fine. Please explain why this is working array size should be supposed to be known at compile time.

Here's the code I have used.

void f(int k)
{
   int a[k];
   .....//some operation
}

int main()
{
  int i = 10;
  f(10);
  return 0;
}

Solution

  • This feature is known as VLA or variable length array. This is not supported in all C standards. In recent C standards like C11 and C99, it is supported, but not in older C Standards as 'C89'.

    If you're using gcc, please have a look at the compiler documentation regarding this.