...

/

Variable Length Arrays

Variable Length Arrays

Learn how to create arrays of variable length without specifying the array size at compile time.

We'll cover the following...

In the array examples earlier, we have hard-coded the size of the array. In modern C, it’s possible to declare arrays without knowing their size until runtime.

Here is how to do it:

Press + to interact
C
#include <stdio.h>
int main(void) {
int n;
// We discuss scanf later. For now, note that n holds the input integer.
scanf("%i", &n);
printf("Entered length: %d\n", n);
int grades[n];
int i;
for (i = 0; i < n; i++) {
grades[i] = i;
printf("grades[%d] = %d\n", i, grades[i]);
}
return 0;
}

We haven’t ...