我正在使用 Linux 计算机。有没有系统命令可以查找我正在使用的 C 编译器所遵循的标准?
我正在使用 Linux 计算机。有没有系统命令可以查找我正在使用的 C 编译器所遵循的标准?
这取决于编译器,我假设你正在使用的是GCC。
你可以使用以下方法检查编译器定义的宏:
gcc -dM -E - < /dev/null
请查看有关标志的手册,特别是:这里
###__STDC_VERSION__###
该宏扩展到C标准版本号,为一个长整型常量,其形式为yyyymmL,其中yyyy和mm是标准版本的年份和月份。这表示编译器符合哪个版本的C标准。与__STDC__一样,这不一定准确地反映了整个实现,除非使用GCC的GNU CPP。
值199409L表示1989 C标准作为1994年修订版,这是当前默认值;值199901L表示C标准的1999年修订版。对1999年修订版的支持尚未完成。
如果使用-traditional-cpp选项,则未定义此宏,编译C++或Objective-C时也未定义此宏。
您还可以使用标准宏在代码中进行测试,例如(最初来自同名的sourceforge项目):
#if defined(__STDC__)
# define PREDEF_STANDARD_C_1989
# if defined(__STDC_VERSION__)
# define PREDEF_STANDARD_C_1990
# if (__STDC_VERSION__ >= 199409L)
# define PREDEF_STANDARD_C_1994
# endif
# if (__STDC_VERSION__ >= 199901L)
# define PREDEF_STANDARD_C_1999
# endif
# if (__STDC_VERSION__ >= 201710L)
# define PREDEF_STANDARD_C_2018
# endif
# endif
#endif
如果你想从命令行检查这个,你可以选择一个(例如c89)并检查最小程序的返回值:
echo -e "#ifdef __STDC__\n#error\n#endif"|gcc -xc -c - > /dev/null 2>&1; test $? -eq 0 || echo "c89
在编译时,检查预处理器宏:
__ANSI__
__STDC__
__STDC_VERSION__
>= 199901L 代表 c99$ gcc -Wall -std=c89 foo.c -o foo
或者:
$ gcc -Wall -std=c99 foo.c -o foo
类型:
$ man gcc
了解完整详情,请查看。
为确定编译器从命令行支持的C版本,请键入:
gcc -dM -E - < /dev/null | grep "__STDC_"
使用您想要检查的编译器替换gcc。
如果编译器支持C89(也称为ANSI C)或ISO C90,则根据标准,您将看到__STDC__
定义为1。(但有时__STDC__
设置为其他非零值)。
C89与C90相同。 C89由ANSI批准。 一年后,ISO批准了ANSI标准。 ISO标准称为C90。
ISO C90标准已被修改,非正式称为C95。(有时也称为C94。但C95更常用)。 ISO还批准了C99,C11(2011年)和C17(2017年)。
如果编译器支持C95或更高版本,您将看到__STDC_VERSION__
被定义。该值将根据版本而异。(例如,C99将使__STDC_VERSION__
被定义为199901L的值)。请参见https://sourceforge.net/p/predef/wiki/Standards/
如果您想在C程序中检查版本,请尝试使用此代码:#include <stdio.h>
#include <math.h> // Needed for INFINITY, HUGE_VAL, HUGE_VALF & HUGE_VALL
// constants (or macros)
#if !defined(__STDC__)
# define __STDC__ 0
#endif
#if !defined(__STDC_VERSION__)
# define __STDC_VERSION__ 0
#endif
int main()
{
if (!__STDC__ && !__STDC_VERSION__) printf("The C compiler does not comply with the C89 or later standard!\nIt likely complies with the 1978 K&R C standard (informally known as C78).\n");
else if (__STDC_VERSION__ >= 201710L) printf("The C compiler complies with the C17 standard.\n");
else if (__STDC_VERSION__ >= 201112L) printf("The C compiler complies with the C11 standard.\n");
else if (__STDC_VERSION__ >= 199901L) printf("The C compiler complies with the C99 standard.\n");
else if (__STDC_VERSION__ >= 199409L) printf("The C compiler complies with the amended C90 standard (also known as C95).\n");
else if (__STDC__) printf("The C compiler complies with the ANSI C89 / ISO C90 standard.\n");
puts("");
if (__STDC__) printf("\"__STDC__\": %ld\n", __STDC_VERSION__);
if (__STDC_VERSION__) printf("\"__STDC_VERSION__\": %ld\n\n", __STDC_VERSION__);
puts("");
if (__STDC_VERSION__ >= 199901L) printf(" INFINITY (added in C99): %f\n", INFINITY ); // Also works with %lf and %Lf
if (__STDC_VERSION__ >= 199901L) printf("-INFINITY (added in C99): %f\n", -INFINITY ); // Also works with %lf and %Lf
puts("");
if (__STDC_VERSION__ >= 199901L) printf(" HUGE_VALF (added in C99): %f\n", HUGE_VALF );
if (__STDC_VERSION__ >= 199901L) printf("-HUGE_VALF (added in C99): %f\n", -HUGE_VALF );
puts("");
if (__STDC__) printf(" HUGE_VAL (added in C89 (ANSI) which is the same as C90 (ISO)): %lf\n", HUGE_VAL );
if (__STDC__) printf("-HUGE_VAL (added in C89 (ANSI) which is the same as C90 (ISO)): %lf\n", -HUGE_VAL );
puts("");
if (__STDC_VERSION__ >= 199901L) printf(" HUGE_VALL (added in C99): %Lf\n", HUGE_VALL );
if (__STDC_VERSION__ >= 199901L) printf("-HUGE_VALL (added in C99): %Lf\n", -HUGE_VALL );
return 0;
}
The C compiler complies with the C99 standard.
"__STDC__": 199901
"__STDC_VERSION__": 199901
INFINITY (added in C99): inf
-INFINITY (added in C99): -inf
HUGE_VALF (added in C99): inf
-HUGE_VALF (added in C99): -inf
HUGE_VAL (added in C89 (ANSI) which is the same as C90 (ISO)): inf
-HUGE_VAL (added in C89 (ANSI) which is the same as C90 (ISO)): -inf
HUGE_VALL (added in C99): inf
-HUGE_VALL (added in C99): -inf
-std
选项来指定要遵循的C标准。默认值为gnu89。没有一般性的系统命令可以确定任何给定编译器的标准。你需要检查文档。__STDC_VERSION__
在gcc默认标准(gnu89)下未定义。 - ciaron#if defined(__STDC__)
# if defined(__STDC_VERSION__)
# if (__STDC_VERSION__ >= 201710L)
# define C_LANGUAGE_STANDARD 2018
# elif (__STDC_VERSION__ >= 201112L)
# define C_LANGUAGE_STANDARD 2011
# elif (__STDC_VERSION__ >= 199901L)
# define C_LANGUAGE_STANDARD 1999
# elif (__STDC_VERSION__ >= 199409L)
# define C_LANGUAGE_STANDARD 1995
# endif
# else
# define C_LANGUAGE_STANDARD 1990
# endif
#else
# define C_LANGUAGE_STANDARD 1972
#endif
更合适呢?
__STDC__
。C89和C90都没有使用__STDC_VERSION__
。请参见https://sourceforge.net/p/predef/wiki/Standards/。 - RobK__STDC_VERSION__
将被设置为199409L。请参见https://sourceforge.net/p/predef/wiki/Standards/。(此网站将C95称为C94,有些人这样做。但C95更常用)。 - RobK
gcc -std=c90
编译时,宏__STDC_VERSION__
未被定义。此外,除非我弄错了,c89 和 c90 是相同的标准...是吗? - einpoklum