0

I'm starting upgrading my C knowledge from C99 to C11, and I wonder if my compiler is capable by default to understand C11 or I need the -std=c11 flag; how can I test it with a simple c11 source code not compiling with -std=c99 flag? There is also the C++11 standard, but I am starting from this article which is only about C.

I tried with some simple code using auto declaration, but since it is a valid keyword also in C99 I'm a bit lost and asking here.

After publishing this question I received answers regarding the macro __STDC__VERSION__, and also tried the code of this answer.

Surprisingly, the outputs of my tests were:

compile line: gcc main.c -std=c11 -o main.exe
oputput of main.exe: c11

compile line: gcc main.c -std=c99 -o main.exe
oputput of main.exe: c99

compile line: gcc main.c -o main.exe
oputput of main.exe: gnu90

The situation is that I can get the compiler capabilities at runtime, but still no at compile time.

For C++ I was able to find this sample code that does compile only with C++11:

int main()
{
    int *p = nullptr;
}

As the nullptr is only available within C++11.

Community
  • 1
  • 1
Zac
  • 4,510
  • 3
  • 36
  • 44
  • Regarding the `auto` keyword, I think you are mixing [C11](https://en.wikipedia.org/wiki/C11_(C_standard_revision)) and [C++11](https://en.wikipedia.org/wiki/C%2B%2B11). C11 did not add any `auto` type deduction, C++11 did. C and C++ are two *very* different languages, they just share some common syntax. – Some programmer dude Jun 15 '16 at 09:38

2 Answers2

2

GCC from 4.x-something started to add C11 support. They have gradually added more and more C11 support. But all 4.x versions default to -std=gnu90.

GCC from 5.0 and newer should default to -std=gnu11 and will understand C11. So in order to know what your gcc defaults to, simply check gcc --version.

Note that this is not the same thing as standard C. If you want pure standard C, you always have to use -std=c11 -pedantic-errors. I would strongly recommend to always use these two switches, particularly when learning C.

Programatically, you can check which version that the compiler is set to like this:

#if __STDC_VERSION__ == 201112L
  #error C11 compiler
#if __STDC_VERSION__ == 199901L
  #error C99 compiler
#else    
  #error Sucky compiler
#endif
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Although this will denote compilers implementing future standards as Sucky too. You luddite! Btw shouldn't you have two following underscores too or is my answer incorrect? – Bathsheba Jun 15 '16 at 09:39
  • @Bathsheba Feel free to add `#if __STDC_VERSION__ > 201112L #error Futuristic compiler #endif`. Indeed there should be 2 trailling underscores, thanks – Lundin Jun 15 '16 at 09:40
  • But are the version numbers guaranteed to be increasing? – Bathsheba Jun 15 '16 at 09:41
  • 1
    @Bathsheba Yeah they are. Though we are gonna get overflow problems and therefore a severe millennium bug in the year 2147483647. – Lundin Jun 15 '16 at 09:43
0

__STDC_VERSION__ is defined with value 201112L for C11. C99 has 199901L as the value.

That will be the most reliable way.

Note that this value will change in future standards and (crediting @Lundin), future version numbers increase monotonically in time.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483