Reputation: 681
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__
.
Upvotes: 4
Views: 3679
Reputation: 153338
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
Upvotes: 5
Reputation: 72629
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?
Upvotes: 5