c - Are variable-length arrays really not allowed in C90? -


i'm reading vlas in c primer plus, , book strictly says introduction of vlas c began c99 standard. whenever attempt declare loop control variable within header of loop, gcc informs me action allowed in c99 mode. however, following test code compiles , works (although prints garbage variables, expected considering none of array elements initialized).

#include <stdio.h>  int main(){     int x;      int = 9;      int array[i];       for(x = 0; x < i; x++)         printf("%d\n", array[x]);      return 0;  } 

if i'm not in c99 mode, how possibly legal?

the book correct, variable length arrays have been supported since c99 , if build following options:

gcc -std=c89 -pedantic

you receive warning:

warning: iso c90 forbids variable length array ‘array’ [-wvla]

if want error can use -pedantic-errors. gcc supported extension before c99, can build explicitly in c99 mode , see no errors:

gcc -std=c99 -pedantic

the language standards supported gcc pages goes details standard gcc supports c , states that:

by default, gcc provides extensions c language on rare occasions conflict c standard


Comments