3

Newbie in C programming.
In gcc -std sets the C standard that compiles, e.g. gcc -std=c99.
It's possible to know which C standard is currently set?

Salvador
  • 786
  • 8
  • 21
  • http://stackoverflow.com/questions/14737104/what-is-the-default-c-mode-for-the-current-gcc-especially-on-ubuntu – ooga Apr 08 '14 at 22:05
  • @ooga You are right, it's exactly the same question. However in all answers there I find how to set the C standard, not how to retrieve it. – Salvador Apr 09 '14 at 18:18

2 Answers2

5

There are various preprocessor symbols that are defined in various modes. You can use gcc -E -dM -x c /dev/null to get a dump of all the preprocessor symbols that are predefined.

When in C99 mode (-std=c99 or -std=gnu99), the symbol __STDC_VERSION__ is defined to be 199901L. In C11 mode (with -std=c11 or std=gnu11), it's 201112L

When in strict C mode (-std=cXX as opposed to -std=gnuXX), the symbol __STRICT_ANSI__ is defined to be 1

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • `gcc -E -dM` gives me _gcc: fatal error: no input files. compilation terminated._ both in Ubuntu and Windows. – Salvador Apr 09 '14 at 19:06
  • @Salvador: well yes, you need to give the compiler driver at least one input file so it knows which language to compile. You can give it any existing file, or create an empty one with a `.c` or `.cpp` extension. – Chris Dodd Apr 10 '14 at 15:45
  • Sorry but newbies need ready to use code as `echo | cpp -dM` that dumps the same predefined preprocessor settings (and doesn't need a file to compile). – Salvador Apr 15 '14 at 20:09
3

You can use this program to print the default:

#include <stdio.h>

int main() {
#ifdef __STRICT_ANSI__
    printf("c");
#else
    printf("gnu");
#endif

#ifdef __STDC_VERSION__
  #if __STDC_VERSION__ == 199901L
    puts("99");
  #elif __STDC_VERSION__ == 201112L
    puts("11");
  #else
    puts("(unknown)");
  #endif
#else
  puts("90");
#endif
  return 0;
}
ooga
  • 15,423
  • 2
  • 20
  • 21