问题描述
我编写了一个简单的C程序来测试 _Generic 关键字的可用性。
I wrote a simple C program to test the availability of _Generic keyword.
int main() {
int _Generic;
}
我用 gcc-5.3.1运行了程序>和 clang-3.8.0 在Ubuntu上进行编译。
I ran the program with gcc-5.3.1 and clang-3.8.0 compilers on Ubuntu.
很明显,该程序在最新的c11标准中编译时会产生错误。
Obviously this program generated an error when compiled in the latest c11 standard.
但是,当使用 -std = c90 和 -std = c99 标志进行编译时,也会产生错误。 _Generic关键字仅在c11标准中引入。
But, when compiled with -std=c90 and -std=c99 flags, it generated an error as well. Whereas, the _Generic keyword was only introduced in c11 standard.
-std =标志的行为是否不同?有没有一种方法可以测试纯c90和c99标准?
Is it that the -std= flags behave differently? And is there a way to test the pure c90 and c99 standards?
EDIT:
我确实使用相同的标识符(不是c11标准的关键字)运行同一程序。像这样:
I did run the same program with other identifiers which are not keywords as per c11 standard. Like:
int _Hello;
int _Gener;
它们成功编译,没有任何错误或警告。
这可能是因为7.1.3,它表示
And they compiled successfully without any errors or warnings.This is probably because of 7.1.3, which says
,如@Art和@Lundin所说。
as said by @Art and @Lundin.
推荐答案
因为不允许使用以下划线和大写字母开头的标识符。 7.1.3:
Because you are not allowed to use identifiers that start with an underscore followed by an upper-case letter. 7.1.3:
此规则自C90起就存在,并不是什么新鲜事。
This rule has been there since C90 and is nothing new.
一种更可靠的测试标准版本的方法是使用为此目的定义的标准宏:
A more reliable way to test for standard version would be to use the standard macros defined for that very purpose:
#ifndef __STDC__
#error Not a standard C compiler.
#endif
#ifdef __STD_VERSION__
#if (__STDC_VERSION__ == 199409L)
/* C95 */
#elif (__STDC_VERSION__ == 199901L)
/* C99 */
#elif (__STDC_VERSION__ == 201112L)
/* C11 */
#else
/* Cxx, unknown future standard */
#endif
#else /* __STDC__ defined but not __STD_VERSION__ */
/* C90 */
#endif
这篇关于为什么在c99或c90模式下支持_Generic关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!