0

In let us C it is mentioned that "while assigning an array we have to commit to the size of the array at the time of writing the programs". But in the code below I'm giving the size at the time of execution still it's working so which is correct ?

code :

#include <stdio.h>
void main(){


{
    int n,i;
    printf("give the size of the array \n");
    scanf("%d",&n);
    int  a[n];
    printf ("give array elements\n");
    for (i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}       
  • 1
    It is said here: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list that the book you are using "is a horribly outdated book that teaches Turbo C and has lot of obsolete, misleading and downright incorrect material". – babon Dec 18 '17 at 11:50

2 Answers2

2

That's a variable-length array and is perfectly valid in C (since the C99 standard).

You should probably update your books. Here's a list of good ones.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

First of all, I think you meant declaration instead of assignment.

That said, this is called variable length array or VLA. It's a C99 onwards addition.

Quoting C11, chapter §6.7.6.2/P4

If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261