Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of an enum whose values appear to be strings?

I am working with Apple's ScriptingBridge framework, and have generated a header file for iTunes that contains several enums like this:

typedef enum {
    iTunesESrcLibrary = 'kLib',
    iTunesESrcIPod = 'kPod',
    iTunesESrcAudioCD = 'kACD',
    iTunesESrcMP3CD = 'kMCD',
    iTunesESrcDevice = 'kDev',
    iTunesESrcRadioTuner = 'kTun',
    iTunesESrcSharedLibrary = 'kShd',
    iTunesESrcUnknown = 'kUnk'
} iTunesESrc;

My understanding was that enum values had to be integer-like, but this definition seems to violate that rule. Furthermore, it seems as though treating these enum values as integers (in an NSPredicate, for example) doesn't do the right thing.

I added the enum declaration above to a C file with an empty main function, and it compiled using i686-apple-darwin9-gcc-4.0.1. So, while these kinds of enums may not conform to the C standard (as Parappa points out below), they are at least being compiled to some type by gcc.

So, what is that type, and how can I use it, for instance, in a format string?

like image 630
Evan DiBiase Avatar asked Feb 14 '09 00:02

Evan DiBiase


People also ask

Can enum have string values?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

What is a string enum?

String Enum String enums are similar to numeric enums, except that the enum values are initialized with string values rather than numeric values. The benefits of using string enums is that string enums offer better readability.

Can I store string in enum?

Enum constants can only be of ordinal types ( int by default), so you can't have string constants in enums.

How do I convert a string to enum in TypeScript?

To convert a string to an enum: Use keyof typeof to cast the string to the type of the enum. Use bracket notation to access the corresponding value of the string in the enum.


2 Answers

C99, TC3 reads:

6.4.4.4 §2:

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. [...]

6.4.4.4 §10:

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

In most implementations, it's safe to use integer character constants of up to 4 one-byte characters. The actual value might differ between different systems (endianness?) though.


This is actually already defined in the ANSI-C89 standard, section 3.1.3.4:

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x' or 'ab'. [...]

An integer character constant has type int. The value of an integer character constant containing a single character that maps into a member of the basic execution character set is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character, or containing a character or escape sequence not represented in the basic execution character set, is implementation-defined. In particular, in an implementation in which type char has the same range of values as signed char, the high-order bit position of a single-character integer character constant is treated as a sign bit.

like image 199
Christoph Avatar answered Oct 08 '22 03:10

Christoph


The single quotes indicate characters, rather than strings in C. So each of the enums will have a 32 bit value consisting of the character codes for the four characters. Actual value will depend in character encodings, but I am assuming 8 bit characters. Note there is no appended \0.

You can use the enums for normal comparison/assignment purposes. As with any enum the underlying type is integer.

I've used this technique in embedded systems many times to create 4 character 'names' that were human readable in hex dump/debugger contexts.

like image 40
Steve Fallows Avatar answered Oct 08 '22 03:10

Steve Fallows