问题
Newbie in C programming.
In gcc -std
sets the C standard that compiles, e.g. gcc -std=c99
.
It's possible to know which C standard is currently set?
回答1:
You can use this program to print the default:
#include <stdio.h>
int main() {
#ifdef __STRICT_ANSI__
printf("c");
#else
printf("gnu");
#endif
#ifdef __STDC_VERSION__
#if __STDC_VERSION__ == 199901L
puts("99");
#elif __STDC_VERSION__ == 201112L
puts("11");
#else
puts("(unknown)");
#endif
#else
puts("90");
#endif
return 0;
}
回答2:
There are various preprocessor symbols that are defined in various modes. You can use gcc -E -dM -x c /dev/null
to get a dump of all the preprocessor symbols that are predefined.
When in C99 mode (-std=c99
or -std=gnu99
), the symbol __STDC_VERSION__
is defined to be 199901L
. In C11 mode (with -std=c11
or std=gnu11
), it's 201112L
When in strict C mode (-std=cXX
as opposed to -std=gnuXX
), the symbol __STRICT_ANSI__
is defined to be 1
来源:https://stackoverflow.com/questions/22949060/how-to-get-current-c-dialect-in-gcc