#include <stdio.h>
int main () 
{
    int size,i;
    int arr[size];
    printf ("Enter size of array\n");
    scanf ("%d",arr[size]);
    for (i=0; i<=size; i++)
    {
        printf ("%d", arr[size]);
    }
    return 0;
}
            Asked
            
        
        
            Active
            
        
            Viewed 1,165 times
        
    0
            
            
         
    
    
        Vlad from Moscow
        
- 301,070
- 26
- 186
- 335
 
    
    
        Aditya Parihar
        
- 1
- 4
- 
                    You can't create an array of size determined at runtime this way. `size` must be a literal - not a variable typed in by the user. – i spin and then i lock Dec 03 '21 at 14:51
- 
                    1`size` is of size unknown, you must assign value before using to define size of an `array`. – TruthSeeker Dec 03 '21 at 14:52
- 
                    1@kenticent: C supports VLA's – TruthSeeker Dec 03 '21 at 14:52
- 
                    @TruthSeeker Which standard? – i spin and then i lock Dec 03 '21 at 14:53
- 
                    @kenticent maybe this help: https://en.wikipedia.org/wiki/Variable-length_array – justANewb stands with Ukraine Dec 03 '21 at 14:55
- 
                    @kenticent, C99, [refer](https://en.cppreference.com/w/c/language/array) – TruthSeeker Dec 03 '21 at 14:57
- 
                    @justANewbie I was hinting that VLA is in the first place a C99 feature, which is not obligatory in the further standards. On the Wikipedia it is said, that "In C11, a __STDC_NO_VLA__ macro is defined if VLA is not supported.", therefore I'm asking, which standard does OP use. – i spin and then i lock Dec 03 '21 at 14:59
- 
                    Possible duplicate of [Dynamic array allocation on stack in C](https://stackoverflow.com/questions/26441916/dynamic-array-allocation-on-stack-in-c) – Wyck Dec 03 '21 at 15:07
- 
                    1Using a VLA is (IMHO) the least of the problems present in this code. – n. m. could be an AI Dec 03 '21 at 15:20
1 Answers
3
            
            
        I have not understood the question but your code is invalid.
Before declaring the variable-length array arr the variable size must to have already a positive value.  So you need to write at least like
int size,i ;
printf ("Enter size of array\n") ; 
scanf ("%d", &size) ;
int arr[size] ;
This call of scanf
scanf ("%d",arr[size]);
does not make any sense, not least because the second argument of the call must be a pointer.
Also, the condition in the for loop must look like
for (i=0; i <size; i++) {
          ^^^^^^^
And you are trying to output a non-existent element of the array
printf ("%d", arr[size]);
The valid range of indices for this variable-length array is [0, size).
It seems you mean
printf ("%d ", arr[i]);
But before outputting elements of the array you need to assign values to them because the array is not initialized and you may not initialize a variable-length array at its declaration.
 
    
    
        Jonathan Leffler
        
- 730,956
- 141
- 904
- 1,278
 
    
    
        Vlad from Moscow
        
- 301,070
- 26
- 186
- 335