枚举的类型是什么类型,其值看起来是字符串? [英] What is the type of an enum whose values appear to be strings?

查看:189
本文介绍了枚举的类型是什么类型,其值看起来是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apple的 ScriptingBridge 框架,并为iTunes生成了包含多个枚举的头文件像这样:

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;

我的理解是, enum 是整数,但这个定义似乎违反了那条规则。此外,似乎将这些 enum 值视为整数(例如在 NSPredicate 中)正确的事情。

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.

我添加了 enum 声明到一个空的 main 函数,并使用 i686-apple-darwin9-gcc-4.0.1 编译。因此,虽然这些 enum 可能不符合C标准(如下面的Parappa指出的),但是它们至少被编译为一些 type by gcc。

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?

推荐答案

C99,TC3读为:

C99, TC3 reads:

6.4.4.4§2:

6.4.4.4 §2:


整数字符常数是用单引号括起来的一个或多个多字节字符序列,如'x'。 [...]

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:

6.4.4.4 §10:


整数字符常量具有类型int。包含映射到单字节执行字符的单个字符的整数字符常量的值是解释为整数的映射字符的表示的数值。包含多个字符(例如,'ab')或包含不映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的。如果整数字符常量包含单个字符或转义序列,则其值为具有类型char的对象(其值为单个字符或转义序列的对象)转换为类型int时得到的值。

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.

在大多数实现中,可以安全地使用最多4个单字节字符的整数字符常量。

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.

这实际上已经在ANSI-C89中定义了标准,第3.1.3.4节:

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


整数字符常量是一个或多个
多字节字符的序列,引号,如'x'或'ab'。 [...]

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

整数字符常量类型为int。包含将
映射到基本执行字符集的成员的单个字符的
整数字符常量的值是映射字符的表示的数值
值,解释为
整数。包含比一个字符更多的
的整数字符常量的值,或者包含在基本执行字符集中表示的不是
的字符或转义序列的值是
实现定义的。特别地,在其中
类型char具有与signed char相同的值范围的实现中,单字符整数字符常数的高阶
位位置是
,被视为符号位。

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.

这篇关于枚举的类型是什么类型,其值看起来是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆