Home > Uncategorized > Was a C90, C99, or C11 compiler used?

Was a C90, C99, or C11 compiler used?

How can a program figure out whether it has been compiled with a C90, C99 or C11 compiler?

Support for the // style of commenting was added in C99.

Support for Unicode string literals (e.g., U"Hello World") was added in C11.

Putting these together we get the following:

#include <stdio.h>
 
#define M(U) sizeof(U"s"[0])
 
int main(void)
{
    switch(M("")*2 //**/ 2
                          )
       {
       case 1: printf("C90\n"); break;
       case 2: printf("C99\n"); break;
       case 8: printf("C11\n"); break;
       }
 
}
Categories: Uncategorized Tags: , ,
  1. Dan
    January 4, 2018 21:16 | #1

    I will admit that after reading this, I searched to make sure that there isn’t a standard predefined macro (like __STDC__) that indicates which version is in effect… but it appears that indeed there is not!

  2. January 4, 2018 21:34 | #2

    @Dan

    There is: the predefined macro _ _STDC_VERSION_ _ is specified in post-C90 versions of the C Standard; “The integer constant 201ymmL”.

    “..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.”

    But an equality test requires remembering the month of publication, and a relational test is susceptible to amended publications appearing in intermediate years. Not the most compelling of reasons. I could also claim that the macro approach does not verify that any of the features work 🙂

  1. No trackbacks yet.