I am working with Apple's ScriptingBridge
framework, and have generated a header file for iTunes that contains several enum
s 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 enum
s 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?
That is an Apple extension to C, whichIt basically translates those enums to:EDIT: Sorry, apparently it's valid C. I've only seem them in Mac code, so wrongly assumed that it was Apple specific.
C99, TC3 reads:
6.4.4.4 §2:
6.4.4.4 §10:
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:
As already stated, those are integers declared using character constants.
When an integer is declared using a character constant of more than one character, it is sensitive to the byte order of the machine for which the constant was developed. As all the original Mac APIs were on PPC or earlier machines, they are backwards with respect to Intel Little-Endian machines.
If you are only building for Intel you can just reverse the order by hand.
If you are building a Universal binary you need to use a flipping function such as CFSwapInt32BigToHost.
Failure to correct those codes will leave you with code that could only work on PowerPC machines, regardless of the lack of compiler errors.
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.