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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.