问题
C++ has a __cplusplus
preprocessor define that lets you detect the version. Is there anything similar for C?
Preferably I'd like it to be portable across XCode, GCC, and Visual Studio versions.
回答1:
As per article on Wikipedia on C99
A standard macro __STDC_VERSION__ is defined with value 199901L to indicate that C99 support is available
#if __STDC_VERSION__ >= 199901L
/*C99*/
#else
/*Not C99*/
#endif
回答2:
You can test the value of the macro __STDC_VERSION__
(note there are two underscores in the beginning and in the end), it should be larger than or equal to 199901L
for C99 compatible platforms.
C11(ISO/IEC 9899:201x) §6.10.8.1 Mandatory macros
__STDC_VERSION__
The integer constant201ymmL
.
In the footnote:
This macro was not specified in ISO/IEC 9899:1990 and was specified as
199409L
in ISO/IEC 9899/AMD1:1995 and as199901L
in ISO/IEC 9899:1999. The intention is that this will remain an integer constant of typelong int
that is increased with each revision of this International Standard.
来源:https://stackoverflow.com/questions/19674401/is-there-a-preprocessor-macro-to-detect-c99-across-platforms