简体   繁体   English

为什么在c99或c90模式下支持_Generic关键字?

[英]Why is _Generic keyword supported in c99 or c90 modes?

I wrote a simple C program to test the availability of _Generic keyword. 我编写了一个简单的C程序来测试_Generic关键字的可用性。

int main() {
    int _Generic;
}

I ran the program with gcc-5.3.1 and clang-3.8.0 compilers on Ubuntu. 我在Ubuntu上使用gcc-5.3.1clang-3.8.0编译器运行了该程序。

Obviously this program generated an error when compiled in the latest c11 standard. 显然,此程序在最新的c11标准中编译时会产生错误。

But, when compiled with -std=c90 and -std=c99 flags, it generated an error as well. 但是,当使用-std = c90-std = c99标志进行编译时,它也会产生错误。 Whereas, the _Generic keyword was only introduced in c11 standard. 而_Generic关键字仅在c11标准中引入。

Is it that the -std= flags behave differently? -std =标志的行为是否有所不同? And is there a way to test the pure c90 and c99 standards? 有没有一种方法可以测试纯c90和c99标准?

EDIT: 编辑:

I did run the same program with other identifiers which are not keywords as per c11 standard. 我确实使用其他标识符(不是c11标准的关键字)运行同一程序。 Like: 喜欢:

int _Hello;
int _Gener;

And they compiled successfully without any errors or warnings. 并且他们编译成功,没有任何错误或警告。 This is probably because of 7.1.3, which says 这可能是因为7.1.3,

If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined 如果程序在保留它的上下文中声明或定义标识符(而不是7.1.4允许),或者将保留标识符定义为宏名,则行为未定义

as said by @Art and @Lundin. 如@Art和@Lundin所说。

Because you are not allowed to use identifiers that start with an underscore followed by an upper-case letter. 因为不允许使用以下划线后跟大写字母开头的标识符。 7.1.3: 7.1.3:

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. 以下划线,大写字母或另一个下划线开头的所有标识符始终保留供任何使用。

This rule has been there since C90 and is nothing new. 自C90以来,就一直存在该规则,这并不是什么新鲜事物。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM