5

I'm reading about VLAs in C Primer Plus, and this book strictly says that the introduction of VLAs to C began with the C99 standard. Whenever I attempt to declare a loop control variable within the header of a for loop, gcc informs me that this action only allowed in C99 mode. However, the following test code compiles and works (although it prints garbage variables, which is to be expected considering none of the array elements were initialized).

#include <stdio.h>

int main(){
    int x; 
    int i = 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 could this possibly be legal?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Theo Chronic
  • 1,623
  • 2
  • 15
  • 16

3 Answers3

9

The book is correct, variable length arrays have been supported since C99 and if you build with the following options:

gcc -std=c89 -pedantic

you will receive an warning:

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

If you want this to be an error you can use -pedantic-errors. gcc supported this as an extension before c99, you can build explicitly in c99 mode and you will see no errors:

gcc -std=c99 -pedantic

The Language Standards Supported by GCC pages goes into details about which standard gcc supports for C and it states that:

By default, GCC provides some extensions to the C language that on rare occasions conflict with the C standard

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
8

If I'm not in C99 mode, how could this possibly be legal?

It’s not. However, GCC allows it as a compiler extension.

You can force GCC to be strict about this by passing the -pedantic flag:

$ gcc -std=c89 -pedantic main.c
main.c: In function ‘main’:
main.c:6: warning: ISO C90 forbids variable-size array ‘array’
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    Of course the implication here is clear: **always compile with `-pedantic`** – and, for that matter, `-Wall -Wextra -Werror` – unless there is a compelling reason (legacy code which would break) not to. – Konrad Rudolph Jul 15 '13 at 12:50
  • `-pedantic` can be pretty annoying about unused parameters. Clang also has a `-Weverything` flag, which can get pretty painful pretty fast. – Carl Norum Jul 15 '13 at 14:14
  • 1
    Use `-pedantic-errors`. And comment out unused parameters. Or don't use unused parameters... oh wait :P – rubenvb Jul 15 '13 at 14:19
  • @Carl Don’t have unused parameters. – Konrad Rudolph Jul 15 '13 at 14:38
  • @KonradRudolph, that's not always possible. Sometimes you have to implement existing API. – Carl Norum Jul 15 '13 at 14:51
  • @Carl The API consists of signatures, no parameter names there. If you meant including existing headers, there’s an easy solution on most compilers: use `-isystem` rather than `-I` to specify the include location, that way the compiler won’t apply the warnings settings to those headers. – Konrad Rudolph Jul 15 '13 at 14:53
  • @CarlNorum: personally, I don't find clang's `-Weverything` painful if you're starting a project from scratch: makefiles and `-Wno-*` make it easy to exclude specific warnings globally or on a per file level, and as Konrad mentioned, there's `-isystem` for 3rd party headers – Christoph Jul 15 '13 at 14:57
3

“If I'm not in C99 mode, how could this possibly be legal?”

Compilers are permitted to have modes other than strict conformance to the C standard.

In effect, the C standard is just one specification. There is no law saying you must conform to it and no law saying a compiler developer must conform to it.

So a compiler is permitted to define its own variations on a language and to compile using its own specification or even a specification written by some third party.

More than that, the C standard defines the language to be extensible. There are many behaviors that an implementation is permitted to define while remaining conforming with the C standard. The C standard even specifies that a conforming program is any program acceptable to a conforming implementation. This means that a program that uses a compiler extension is still a conforming C program (in the absence of any other issues). (However, it is not a strictly conforming program; those are programs that do not use extensions.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312