0
#include <stdio.h>
//print els of an array

int main(){
    int n,arr[n],i;

    printf("\nenter no. of elements in the array : ");
    scanf("%d",&n);

    printf("\nenter elements of the array : ");
    for(i=0; i < n; i++){
        scanf("%d",&arr[i]);
    }

    for(i=0; i < n; i++){
        printf("\n%d ",arr[i]);
    }

    return 0 ;
}
Yun
  • 3,056
  • 6
  • 9
  • 28

1 Answers1

1

To create a VLA you need to know the length provided by the user first before creating the array, so Just as JustASimpleLonelyProgrammer stated, you should only create the array after reading the value of n.

scanf("%d", &n);
int arr[n];

However, dynamic memory allocation is the more usual solution to this problem.

int* arr = (int*) malloc(n * sizeof(int));

Please refer to this answer for difference between dynamic memory allocation and VLA..