17

What gcc options shall I use to enforce ANSI C (C99) warnings/errors? gcc (GCC) 3.4.2 (mingw-special)

I'm using: gcc -pedantic -ansi -std=c99 is this correct?

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
AlfaTeK
  • 7,487
  • 14
  • 49
  • 90
  • 1
    ANSI published the original C standard in 1989. ISO published its version of the same standard in 1990, and ANSI officially adopted that version. ISO published new editions of the standard in 1999 and 2011, and ANSI has adopted each of them, making the earlier editions officially obsolete. Confusingly, the term "ANSI C" is still commonly (and incorrectly) used to refer to the language defined by the 1989/1990 editions, and `gcc -ansi` still refers to that version. I suggest that the term "ANSI C" is ambiguous and should be avoided. Refer to C89/C90, C99, or C11 instead. – Keith Thompson Feb 02 '15 at 15:25

3 Answers3

33

The -ansi flag is synonymous with the -std=c89 flag.

Just using -std=c99 with -pedantic should be sufficient.

When in doubt, you can always refer to the GCC documentation. As of GCC 3.4.2, the chapter to read is 2 - Language Standards Supported by GCC.

sleske
  • 81,358
  • 34
  • 189
  • 227
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • ... `-std=c99` with `-pedantic`... : I don't understand the difference between this and `-std=c89`, do you meant those `-ansi` errors would show as warnings? – NeoZoom.lua Feb 17 '21 at 04:24
8

This is an old question but I just wanted to add some extra points.

Firstly, regardless of the set of generic command-line switches you supply to GCC, currently it doesn't appear to be possible to make GCC to report all constraint violations as "errors" and everything else as "warnings". Some of the diagnostic messages GCC reports as "warnings" are in fact constraint violations (i.e. "errors") from the point of view of C language, but there's no way to force GCC to recognize that fact and generate an "error" diagnostic. Quite possibly that a more precise separation can be achieved by fine-tuning individual warning types, but I'm not sure GCC settings provide sufficient granularity to achieve a good match.

Secondly, GCC provides -pedantic-errors option that can be used in place of plain -pedantic, which is intended to enable a more precise (as described above) classification of diagnostic messages into "errors" and "warnings". It is still not perfect though.

P.S. The language specification doesn't require/define the separation of diagnostic messages into "errors" and "warnings", but in practice many programmers expect constraint violations to be reported as "errors". I thought that you might have meant something like that when you mentioned "enforcing warnings/errors" in your question.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    Good observations. At any rate, I believe since C allows so many dangerous things, the only sane way to work is to handle *any* warning as an error (i.e. you're not done until it builds with `-Werror`). Then it does not matter if some errors are misreported as warnings. – sleske Feb 02 '15 at 09:18
4
-ansi
    In C mode, this is equivalent to -std=c89. In C++ mode, it is equivalent to -std=c++98.

ANSI C isn't the same as C99 (yet). Also, -Wall might also be of interest, but only -pedantic should do what you want.

Mikael S
  • 5,206
  • 2
  • 23
  • 21
  • 6
    "ANSI C isn't the same as C99". ANSI has adopted C99 as a standard. It's just that for whatever reason, C89 came to be called "ANSI C", to distinguish it from pre-standard C, and the name has stuck around. – Steve Jessop Nov 30 '09 at 23:01