简体   繁体   中英

Why is _Generic keyword supported in c99 or c90 modes?

I wrote a simple C program to test the availability of _Generic keyword.

int main() {
    int _Generic;
}

I ran the program with gcc-5.3.1 and clang-3.8.0 compilers on Ubuntu.

Obviously this program generated an error when compiled in the latest c11 standard.

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.

Is it that the -std= flags behave differently? And is there a way to test the pure c90 and c99 standards?

EDIT:

I did run the same program with other identifiers which are not keywords as per c11 standard. Like:

int _Hello;
int _Gener;

And they compiled successfully without any errors or warnings. This is probably because of 7.1.3, which says

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

as said by @Art and @Lundin.

Because you are not allowed to use identifiers that start with an underscore followed by an upper-case letter. 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.


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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