4

Is there any piece of code I can write or any other way to check which version of the C language my compiler is compiling?

I was wondering if there was a way for me to see if the compiler in question is C89, C99, C11, C18 etc. Perhaps utilizing __STDC_VERSION__.

wovano
  • 4,543
  • 5
  • 22
  • 49
peki
  • 669
  • 7
  • 24
  • @pzaenger thanks for that, but I was wondering if there was a way for me to see if the compiler in question is C89, C99, C11, C18 etc. Perhaps utilizing `__STDC_VERSION__`. – peki Nov 25 '19 at 21:52
  • Please [edit] to clarify this within the question. It will be more helpful for future users. – Yunnosch Nov 25 '19 at 21:56

2 Answers2

5

You can look at the __STDC_VERSION__ macro, which hast the format YYYYMM and from that deduce whether you run C89, C99, C11 or C18.

See also What is the __STDC_VERSION__ value for C11?

Fredrik
  • 1,389
  • 1
  • 14
  • 32
Jens
  • 69,818
  • 15
  • 125
  • 179
5

How to check which version of C my compiler is using?

To check against standard versions, use __STDC__ and __STDC_VERSION__. Various compilers also offer implementation specific macros for further refinement.

__STDC__ available with C89 version and onward.

Compliant versions prior to C94 do not certainly define __STDC_VERSION__. Since then it is a long constant.

The common values found include:

199409L
199901L
201112L
201710L

Putting that together

#if defined(__STDC__)
  #if defined(__STDC_VERSION__)
    printf("Version %ld\n", __STDC_VERSION__);
  #else
    puts("Standard C - certainly 1989");
  #endif
#else
  puts("Pre 1989 or non-compliant C");
#endif

Example macro usage

wovano
  • 4,543
  • 5
  • 22
  • 49
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256