C语言 如何找到我当前编译器的标准,比如它是 C90 等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4991707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find my current compiler's standard, like if it is C90, etc
提问by Hemanth
I'm working on a Linux machine. Is there any system command to find the standard followed by the C compiler I'm using?
我在 Linux 机器上工作。是否有任何系统命令可以找到我正在使用的 C 编译器后面的标准?
回答by Tarantula
This is compiler dependent, I'm supposing you're using GCC. You could check your compiler defined macros using:
这取决于编译器,我假设您正在使用 GCC。您可以使用以下方法检查编译器定义的宏:
gcc -dM -E - < /dev/null
Check the manual about the flags, specially:
__STDC_VERSION__
__STDC_VERSION__
This macro expands to the C Standard's version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version. This signifies which version of the C Standard the compiler conforms to. Like STDC, this is not necessarily accurate for the entire implementation, unless GNU CPP is being used with GCC.
The value 199409L signifies the 1989 C standard as amended in 1994, which is the current default; the value 199901L signifies the 1999 revision of the C standard. Support for the 1999 revision is not yet complete.
This macro is not defined if the -traditional-cpp option is used, nor when compiling C++ or Objective-C.
这个宏扩展到 C 标准的版本号,一个 yyyymmL 形式的长整数常量,其中 yyyy 和 mm 是标准版本的年和月。这表示编译器符合哪个版本的 C 标准。与STDC一样,这对于整个实现来说不一定准确,除非 GNU CPP 与 GCC 一起使用。
值 199409L 表示 1994 年修订的 1989 C 标准,这是当前的默认值;值 199901L 表示 C 标准的 1999 修订版。对 1999 修订版的支持尚未完成。
如果使用 -traditional-cpp 选项,则不会定义此宏,也不会在编译 C++ 或 Objective-C 时定义。
In this siteyou can find a lot of information about this. See the table present here.
回答by Flexo
You can also test this in your code using standard macros, for example (originally from sourceforge project of the same name):
例如,您还可以使用标准宏在代码中对此进行测试(最初来自同名的 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
If you want to check this from the command line you can pick one (e.g. c89) and check the return value from a minimal program:
如果您想从命令行检查这一点,您可以选择一个(例如 c89)并检查来自最小程序的返回值:
echo -e "#ifdef __STDC__\n#error\n#endif"|gcc -xc -c - > /dev/null 2>&1; test $? -eq 0 || echo "c89
回答by Yann Droneaud
At compile time, check against preprocessor macro:
在编译时,检查预处理器宏:
__ANSI____STDC____STDC_VERSION__>= 199901L for c99
__ANSI____STDC____STDC_VERSION__>= 199901L for c99
回答by Paul R
You probably have gcc, in which case you can specify the standard at compile-time, e.g.
您可能有 gcc,在这种情况下,您可以在编译时指定标准,例如
$ gcc -Wall -std=c89 foo.c -o foo
or:
或者:
$ gcc -Wall -std=c99 foo.c -o foo
Type:
类型:
$ man gcc
for full details.
详细信息。
回答by ciaron
If your C compiler is gcc, you can use the -stdoption to specifywhich C standard to follow. The default is gnu89. There's no general system command to determine the standard for any given compiler. You'll need to check the documentation.
如果您的 C 编译器是 gcc,您可以使用该-std选项指定要遵循的 C 标准。默认为 gnu89。没有通用的系统命令来确定任何给定编译器的标准。您需要检查文档。

