Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a preprocessor macro to detect C99 across platforms?

Tags:

c

macros

c99

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.

like image 756
Jherico Avatar asked Oct 30 '13 05:10

Jherico


People also ask

What does a preprocessor macro do?

The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be the inclusion of header files, macro expansions, etc.

Is macro and preprocessor same?

Preporcessor: the program that does the preprocessing (file inclusion, macro expansion, conditional compilation). Macro: a word defined by the #define preprocessor directive that evaluates to some other expression. Preprocessor directive: a special #-keyword, recognized by the preprocessor.

What is macro expansion in preprocessor?

Macro expansion overviewThe preprocessor maintains a context stack, implemented as a linked list of cpp_context structures, which together represent the macro expansion state at any one time. The struct cpp_reader member variable context points to the current top of this stack.

What is the use of #ifdef?

The #ifndef directive checks for the opposite of the condition checked by #ifdef . If the identifier hasn't been defined, or if its definition has been removed with #undef , the condition is true (nonzero). Otherwise, the condition is false (0). The identifier can be passed from the command line using the /D option.


2 Answers

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
like image 68
doptimusprime Avatar answered Oct 18 '22 13:10

doptimusprime


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 constant 201ymmL.

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 as 199901L in ISO/IEC 9899:1999. The intention is that this will remain an integer constant of type long int that is increased with each revision of this International Standard.

like image 29
Yu Hao Avatar answered Oct 18 '22 13:10

Yu Hao