What GCC options should be set to have GCC as strict as possible? (and I do mean as strict as possible) I'm writing in C89 and want my code to be ANSI/ISO compliant.
-
For strictest you should be clear which standard you are targeting. ANSI X3.159-1989 and/or ISO/IEC 9899:1990, ISO/IEC 9899:1999, or "C1X" from ISO/IEC working group (http://www.open-std.org/JTC1/SC22/WG14) (JTC1/SC22/WG14). ANSI C and ISO C90 only differ in the section numbering of the standard itself AFAIK – mctylr Jan 20 '12 at 21:07
-
2@mctylr: "I'm writing in C89" seems perfectly clear. – Keith Thompson Jan 20 '12 at 21:26
-
2Strictly speaking, C89 is not ANSI/ISO compliant. The current ISO C standard is the one published in 2011; that's also the current ANSI C standard; the 1989, 1990, and 1999 standards are officially obsolete. But that's just a quibble over wording; there's still widespread support for C89/C90 (more than for C99), and you can still conform to it even if it's no longer an official standard. – Keith Thompson Jan 20 '12 at 21:29
-
@KeithThompson I wasn't sure if there was an implicit _latest_ as in "... want my code to be _latest_ ANSI/ISO standard complaint". – mctylr Jan 25 '12 at 22:05
2 Answers
I'd recommend using:
-Wall -Wextra -std=c89 -pedantic -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition
You should compile with -O
as well as -g
as some warnings are only available when the optimizer is used (actually, I usually use -O3
for spotting the problems). You might prefer -std=gnu89
as that disables fewer extensions in the libraries. OTOH, if you're coding to strict ANSI C89, maybe you want them disabled. The -ansi
option is equivalent to -std=c89
but not quite as explicit or flexible.
The missing prototypes warns you about functions which are used (or external functions defined) without a prototype in scope. The strict prototypes means you can't use 'empty parentheses' for function declarations or definitions (or function pointers); you either need (void)
or the correct argument list. The old style definition spots K&R style function definitions, such as:
int old_style(a, b) int a; double b; { ... }
If you're lucky, you won't need to worry about that. I'm not so lucky at work, and I can't use strict prototypes, much to my chagrin, because there are too many sloppy function pointers around.
See also: What is the best command-line tool to clean up code

- 1
- 1

- 730,956
- 141
- 904
- 1,278
-
1The `-Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition` options are a good idea, but they don't address standard conformance. Old-style function declarations and definitions have been obsolescent since 1989, but they're still part of the language; even C2011-conforming compilers are required to support them. – Keith Thompson Jan 20 '12 at 21:31
-
1@Keith: you have the `-pedantic` flag turned on in your reading :D Yes, but I still recommend using the options so that the code is maximally forward-portable to later versions of the standard. It would not surprise me to find that C2021 finally loses standard support for non-prototype functions, though implementations will likely continue to support the old notations for another 10-20 years after that. C11 also eliminates `gets()`; I don't expect it to vanish from libraries for a long time either. It should be avoided, even though it is fully C89 compliant. – Jonathan Leffler Jan 20 '12 at 21:40
-
I wrote a post on this: http://shitalshah.com/p/how-to-enable-and-use-gcc-strict-mode-compilation/ – Shital Shah Jul 11 '17 at 23:36
-
@ShitalShah: Interesting. Your blog is about C++ rather than C, but that's OK. I'm puzzled about the 'ISO C99 requires rest arguments to be used' message (especially in a C++ compilation). I'm not clear what that means. Is it related to the `__VA_ARGS__` macro — it must be used somewhere? Or is it something else. I looked at the bug report and frankly wasn't enlightened about the change that was required. – Jonathan Leffler Jul 11 '17 at 23:52
This set of options is pretty good:
-Wall -Wextra -ansi -pedantic
You'll have to read the documentation to see if there are any extra warnings getting left out by that combination.
You should be warned that strict C89 doesn't include support for //
style comments, and there are some pretty serious restrictions on the number of significant characters in the names of objects with external linkage.

- 730,956
- 141
- 904
- 1,278

- 219,201
- 40
- 422
- 469
-
1Documentation says that "-ansi -pedantic" should get you strict ISO C90, and will warn on ANY GNU C extension (not just those that are incompatible). You can use "-pedantic-errors" if you want errors. Though not really an issue of strictness, you can consider "-Werror" which will turn warnings into errors. Though philosophically "Warnings are not Errors" – Jon L Jan 20 '12 at 19:33