<-ansi
is an obsolete switch that requests the compiler to compile according to the 30-year-old obsolete revision of C standard, ISO/IEC 9899:1990, which is essentially a rebranding of the ANSI standard X3.159-1989 "Programming Language C. Why obsolete? Because after C90 was published by ISO, ISO has been in charge of the C standardization, and any technical corrigenda to C90 have been standardized by ISO. Thus it is more apt to use the -std=c90
.
Without this switch, the recent GCC C compilers will conform to the C language standardized in ISO/IEC 9899:2011, or the newest 2018 revision.
Unfortunately there are some lazy compiler vendors that believe it is acceptable to stick to an older obsolete standard revision, for which the standardization document is not even available from standard bodies.
Using the switch helps ensuring that the code should compile in these obsolete compilers.
The -pedantic
is an interesting one. In absence of -pedantic
, even when a specific standard is requested, GCC will still allow some extensions that are not acceptable in the C standard. Consider for example the program
struct test {
int zero_size_array[0];
};
The C11 draft n1570 paragraph 6.7.6.2p1 says:
In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero.[...]
The C standard requires that the array length be greater than zero; and this paragraph is in the constraints; the standard says the following 5.1.1.3p1:
A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
However, if you compile the program with gcc -c -std=c90 pedantic_test.c
, no warning is produced.
-pedantic
causes the compiler to actually comply to the C standard; so now it will produce a diagnostic message, as is required by the standard:
gcc -c -pedantic -std=c90 pedantic_test.c
pedantic_test.c:2:9: warning: ISO C forbids zero-size array ‘zero_size_array’ [-Wpedantic]
int zero_size_array[0];
^~~~~~~~~~~~~~~
Thus for maximal portability, specifying the standard revision is not enough, you must also use -pedantic
(or -pedantic-errors
) to ensure that GCC actually does comply to the letter of the standard.
The last part of the question was about using -ansi
with C++. ANSI never standardized the C++ language - only adopting it from ISO, so this makes about as much sense as saying "English as standardized by France". However GCC still seems to accept it for C++, as stupid as it sounds.