Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save colematt/97a3b50b680cfff98456cdcdfe4c721c to your computer and use it in GitHub Desktop.

Select an option

Save colematt/97a3b50b680cfff98456cdcdfe4c721c to your computer and use it in GitHub Desktop.
[Detect C or C++ Standard from Predefined Macros] #c #cpp

See the following:

C

C Standard __STDC__ __STDC_VERSION__
C89 1 #undef
C90 1 #undef
C94 1 199409L
C99 1 199901L
C11 1 201112L
C17 1 201710L
C2X, GNU2x 1 strictly larger than 201710L

C++

C++ Standard __STDC__ __cplusplus __cplusplus_cli
C++98 1 199711L #undef
C++11 1 201103L #undef
C++14 1 201402L #undef
C++17 1 201703L #undef
C++2a, GNU++2a 1 strictly larger than 201703L #undef
C++/CLI #undef 200406L
#include <stdio.h>
int main(int argc, char** argv){
switch(__STDC_VERSION__){
case 199409L:
printf("__STDC_VERSION__ %l (C94)\n");
break;
case 199901L:
printf("__STDC_VERSION__ %l (C99)\n");
break;
case 201112L:
printf("__STDC_VERSION__ %l (C11)\n");
break;
case 201710L:
printf("__STDC_VERSION__ %l (C17)\n");
break;
default:
printf("__STDC_VERSION__ %l");
__STDC_VERSION__ > 201710L ? printf(" (std=c++2a)\n");
: printf(" Unknown standard\n");
}
return 0;
}
#include <iostream>
int main() {
switch (__cplusplus) {
case 199711L:
std::cout << __cplusplus << " (std=c++98)" << std::endl;
break;
case 201103L:
std::cout << __cplusplus << " (std=c++11)" << std::endl;
break;
case 201402L:
std::cout << __cplusplus << " (std=c++14)" << std::endl;
break;
case 201703L:
std::cout << __cplusplus << " (std=c++17)" << std::endl;
break;
default:
std::cout << __cplusplus;
__cplusplus > 201703L ? std::cout << " (std=c++2a)" << std::endl
: std::cout << " Unknown standard" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment