LearnFrenzy
Sign in Take a free test

Arrays · Predict the Output · Question 10

sizeof of a variable length array in C Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 7;
    int v[n];
    n = 100;            /* does this change v? */
    printf("%zu\n", sizeof v);
    return 0;
}
  • A 400
  • B 8
  • C Compilation error
  • D 28 Correct
Correct Answer: Option D

For a VLA, sizeof is worked out at run time from the size the array had when it was created: 7 × 4 = 28. Changing n afterwards does not resize v.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links