简体   繁体   中英

Is assigning a type to an enum standard C?

I have seen enum declarations like this:

enum ProgrammingLanguage: unsigned char {
    C = 0,
    CPlusPlus,
    Rust,
    Java,
    Javascript,
    Python
};

This only allocates one byte for this type. My question is, is this standard C or a GCC extension? Do I need to worry about portability if I decide to do this?

is this standard C

No, this is not a part of C language.

a GCC extension?

No, this is not a GCC extension in C language.

Do I need to worry about portability if I decide to do this?

You need to worry about compiling such code at all with any C compiler.

This is solely C++ feature.

Then it is a macOS feature. Compiling it in Xcode in a C file works

It is a clang extension .

Appendix A 6.7.2.2 defines an enum as:

(6.7.2.2) enum-specifier:
  enum identifier-opt { enumerator-list }
  enum identifier-opt { enumerator-list,}
  enum identifier

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