Next: , Previous: ANSI/ISO, Up: C language standards


3.3.2 Strict ANSI/ISO

The command-line option -pedantic in combination with -ansi will cause gcc to reject all GNU C extensions, not just those that are incompatible with the ANSI/ISO standard. This helps you to write portable programs which follow the ANSI/ISO standard.

Here is a program which uses variable-size arrays, a GNU C extension. The array x[n] is declared with a length specified by the integer variable n.

     int 
     main (int argc, char *argv[])
     {
       int i, n = argc;
       double x[n];
     
       for (i = 0; i < n; i++)
         x[i] = i;
     
       return 0;
     }

This program will compile with -ansi, because support for variable length arrays does not interfere with the compilation of valid ANSI/ISO programs—it is a backwards-compatible extension:

     $ gcc -Wall -ansi gnuarray.c

However, compiling with -ansi -pedantic reports warnings about violations of the ANSI/ISO standard:

     $ gcc -Wall -ansi -pedantic gnuarray.c
     gnuarray.c: In function `main':
     gnuarray.c:5: warning: ISO C90 forbids variable-size
       array `x'

Note that an absence of warnings from -ansi -pedantic does not guarantee that a program strictly conforms to the ANSI/ISO standard. The standard itself specifies only a limited set of circumstances that should generate diagnostics, and these are what -ansi -pedantic reports.