23

I want to do something in C99 one way, otherwise to perform it another way. What is the #define to check for?

#ifdef C99
...
#else
...
#endif
Jens
  • 69,818
  • 15
  • 125
  • 179
klynch
  • 1,076
  • 2
  • 9
  • 15
  • 1
    Terminology nitpick: What newbies call a #define, the gurus call a *macro* or (*macro identifier* when they specifically refer to the thingy that should be replaced). – Jens Oct 02 '11 at 19:01

2 Answers2

38

There is not an specific #define value. Just check __STDC_VERSION__ and define it yourself! ;-)

#if __STDC_VERSION__ >= 199901L
/* C99 code */
#define C99
#else
/* Not C99 code */
#endif


#ifdef C99
/*My code in C99 format*/
#else
/*My code in C99 format*/
#endif

EDIT: A more general snippet, from here. I've just changed the defined names, just in case you'll use them a lot on the code:

#if defined(__STDC__)
# define C89
# if defined(__STDC_VERSION__)
#  define C90
#  if (__STDC_VERSION__ >= 199409L)
#   define C94
#  endif
#  if (__STDC_VERSION__ >= 199901L)
#   define C99
#  endif
#  if (__STDC_VERSION__ >= 201112L)
#   define C11
#  endif
# endif
#endif
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Khelben
  • 6,283
  • 6
  • 33
  • 46
24
#if __STDC_VERSION__ == 199901L
/* C99 */
#else
/* not C99 */
#endif

Change == to >= if you want to test for C99 and later.

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158