Occasionally a valid ANSI/ISO program may be incompatible with the
extensions in GNU C. To deal with this situation, the compiler option
-ansi disables those GNU extensions which are in conflict with the
ANSI/ISO standard. On systems using the GNU C Library (glibc
) it
also disables extensions to the C standard library. This allows
programs written for ANSI/ISO C to be compiled without any unwanted
effects from GNU extensions.
For example, here is a valid ANSI/ISO C program which uses a variable
called asm
:
#include <stdio.h> int main (void) { const char asm[] = "6502"; printf ("the string asm is '%s'\n", asm); return 0; }
The variable name asm
is valid under the ANSI/ISO standard, but
this program will not compile in GNU C because asm
is a GNU C
keyword extension (it allows native assembly instructions to be used in
C functions). Consequently, it cannot be used as a variable name
without giving a compilation error:
$ gcc -Wall ansi.c ansi.c: In function `main': ansi.c:6: parse error before `asm' ansi.c:7: parse error before `asm'
In contrast, using the -ansi option disables the asm
keyword extension, and allows the program above to be compiled
correctly:
$ gcc -Wall -ansi ansi.c $ ./a.out the string asm is '6502'
For reference, the non-standard keywords and macros defined by the GNU C
extensions are asm
, inline
, typeof
, unix
and
vax
. More details can be found in the GCC Reference Manual
“Using GCC” (see Further reading).
The next example shows the effect of the -ansi option on
systems using the GNU C Library, such as GNU/Linux systems. The program
below prints the value of pi, \pi=3.14159..., from the
preprocessor definition M_PI
in the header file math.h:
#include <math.h> #include <stdio.h> int main (void) { printf ("the value of pi is %f\n", M_PI); return 0; }
The constant M_PI
is not part of the ANSI/ISO C standard library
(it comes from the BSD version of Unix). In this case, the program will
not compile with the -ansi option:
$ gcc -Wall -ansi pi.c pi.c: In function `main': pi.c:7: `M_PI' undeclared (first use in this function) pi.c:7: (Each undeclared identifier is reported only once pi.c:7: for each function it appears in.)
The program can be compiled without the -ansi option. In this case both the language and library extensions are enabled by default:
$ gcc -Wall pi.c $ ./a.out the value of pi is 3.141593
It is also possible to compile the program using ANSI/ISO C, by enabling
only the extensions in the GNU C Library itself. This can be achieved
by defining special macros, such as _GNU_SOURCE
, which enable
extensions in the GNU C Library:1
$ gcc -Wall -ansi -D_GNU_SOURCE pi.c $ ./a.out the value of pi is 3.141593
The GNU C Library provides a number of these macros (referred to as
feature test macros) which allow control over the support for
POSIX extensions (_POSIX_C_SOURCE
), BSD extensions
(_BSD_SOURCE
), SVID extensions (_SVID_SOURCE
),
XOPEN extensions (_XOPEN_SOURCE
) and GNU extensions
(_GNU_SOURCE
).
The _GNU_SOURCE
macro enables all the extensions together,
with the POSIX extensions taking precedence over the others in cases
where they conflict. Further information about feature test macros can
be found in the GNU C Library Reference Manual (see Further reading).