Forcing the compiler to the C99 standard

I coded in my project when I discovered that the anonymous structures that I have been using for a while are actually only available in C11, not C99, the standard that I want to code.

Given the following code:

struct data { int a; struct { int b; int c; }; }; int main() { struct data d; da = 0; db = 1; dc = 2; return 0; } 

This code should only compile in C11 (or if compiler extensions provide this feature and are included). So let's see the results on different compilers:

clang 5

 compiler: Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.1.0 Thread model: posix command: clang -std=c99 -Wall test.c -o test result: **OK** 

gcc 4.1

 compiler: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) command: gcc -std=c99 -Wall test.c -o test result: **NOT OK** test.c:6: warning: declaration does not declare anything test.c: In function 'main': test.c:14: error: 'struct data' has no member named 'b' test.c:15: error: 'struct data' has no member named 'c' 

gcc 4.7

 compiler: gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8) command: gcc -std=c99 -Wall test.c -o test result: **OK** test.c: In function 'main': test.c:11:14: warning: variable 'd' set but not used [-Wunused-but-set-variable] 

I always tried to get the compiler to work in C99 mode by specifying -std=c99 , but obviously this will not work (except for gcc 4.1, which compiles fine without the -std option). So now my question is: how can I make the gcc and clang compilers in any version give an error if I write code that does not conform to the standard specified with -std ? Is there any command line argument that I don't know about?

+7
c gcc c99 c11 clang
source share
2 answers

-std=c99 will not disable language extensions (GNU C has anon structures on C99).

The -pedantic (or -pedantic-errors ) flags warn the compiler about language extensions.

+15
source share

Both gcc and clang allow a large number of extensions and here for clang . clang generally tries to support most of the extensions gcc does. Both will only allow you to use C functions in C ++, like VLA, which are a C99 function .

In this case, both allow the use of the concepts struct / union in structures / unions in the C99 mode, even if it is a C11 feature.

The language standards supported by GCC are good, what flags are needed to turn them into warnings and errors, and as far as I know clang follows the same conventions:

[...] in order to get all the diagnostics required by the standard, you must also specify -patent (or -patent-errors if you want them to be errors, not warnings). [...]

So -pedantic warn you if you use the extension, and -pedantic-errors will turn these warnings into an error message. With the -pedantic flag -pedantic you should see a warning similar to this in gcc :

warning: ISO C99 does not support unnamed structures / unions [-Wpedantic]

and this is in clang ( see it live ):

warning: anonymous structures are a C11 extension [-Wc11-extensions]

and it turns into an error with -pedantic-errors .

+8
source share

All Articles