Get Version of C Standard Used to Compile C Code

Get Version of C Standard Used to Compile C Code

When writing or maintaining C code, it's often helpful to know which version of the C standard the compiler is using. This can affect available features, syntax, and standard library functions. This tutorial demonstrates how to get the version of C standard used to compile C code.

The __STDC_VERSION__ is a predefined macro in C compilers (e.g. gcc on Linux) that expands to a long integer constant representing the version of the C standard in use.

#include <stdio.h>

int main()
{
    printf("%ld\n", __STDC_VERSION__);

    return 0;
}

The __STDC_VERSION__ macro holds a value that represents the year and month of the C standard being used. For example:

  • 199901L (C99)
  • 201112L (C11)
  • 201710L (C17)
  • 202311L (C23)
  • other number (pre-standard C)

For instance, an output of 201710L indicates that the code was compiled with the C17 standard.

Keep in mind that not all C compilers define the __STDC_VERSION__ macro consistently. While most modern compilers follow the standard and provide accurate values, some may omit or set it incorrectly.

Leave a Comment

Cancel reply

Your email address will not be published.