Search code examples
cgccccturbo-c

Is this a valid C program?


I wrote a program, where the size of an array is taken as an input from user.

#include <stdio.h>
main()
{
    int x;
    scanf("%d", &x);
    int y[x];
    /* some stuff */
}

This program failed to compile on my school's compiler Turbo C (an antique compiler). But when I tried this on my PC with GNU CC, it compiled successfully.

So my question is, is this a valid C program? Can I set the size of the array using a user's input?


Solution

  • c90 does not support variable length arrays you can see this using this command line:

    gcc -std=c90 -pedantic code.c
    

    you will see an error message like this:

    warning: ISO C90 forbids variable length array ‘y’ [-Wvla]
    

    but c99 this is perfectly valid:

    gcc -std=c99 -pedantic code.c