4
#include <stdio.h>

int main(void)
{
  int a=17;
  scanf("%d",&a);

  int arr[a];

  printf("%lu", sizeof(arr));
}

Memory for array "arr" should be allocated at compile time but in this case it takes the value of "a" from the user(run-time) and allocates the same size for the array. Please Clarify.

unwind
  • 391,730
  • 64
  • 469
  • 606
Raj
  • 51
  • 1

1 Answers1

6

Yes, this is known as a variable-length array. It's been standard in C since C99.

So no, the memory should not be allocated at compile time for code like this. That would be impossible, of course.

Also, values of type size_t (like those generated by the sizeof operator) should be printed using %zu.

unwind
  • 391,730
  • 64
  • 469
  • 606