&是什么QUOT;再presentable"在C11是什么意思? [英] what does "representable" mean in C11?

查看:216
本文介绍了&是什么QUOT;再presentable"在C11是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据C11 WG14草案版本N1570

<&文件ctype.h GT; 声明进行分类有用的几个功能
  和字符映射。在所有情况下的参数为 INT 中,
  其中价值应重新presentable为 unsigned char型或应
  等于宏观 EOF 的价值。如果参数为其他值,
  该行为是不确定的。

The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.

它是未定义的行为?

#include <ctype.h>
#include <limits.h>
#include <stdlib.h>

int main(void) {
  char c = CHAR_MIN; /* let assume that char is signed and CHAR_MIN < 0 */
  return isspace(c) ? EXIT_FAILURE : EXIT_SUCCESS;
}

是否标准允许字符传递给 isspace为()字符 INT )?换句话说,就是字符转换为 INT 之后的再presentable 的作为 unsigned char型

Does the standard allow to pass char to isspace() (char to int)? In other words, is char after conversion to int representable as an unsigned char?

下面的维基如何界定重presentable

能够被重新presented。

Capable of being represented.

字符能够被重新presented为 unsigned char型即可。 §6.2.6.1/ 4:

Is char capable of being represented as unsigned char? Yes. §6.2.6.1/4:

存储在任何其他对象类型的非位字段对象值
  包含的 N × CHAR_BIT 位,这里的 N 的是,一个对象的大小
  型,以字节为单位。的值可以被复制到类型的对象
   unsigned char型 [ N 的(例如,由的memcpy );由此产生的字节集
  叫的对象重新$ P $价值psentation

Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value.

的sizeof(char)的== 1 因此,其对象重新presentation是 unsigned char型[1] 字符是能够被psented为重新$ p $ unsigned char型。我在哪里错了?

sizeof(char) == 1 therefore its object representation is unsigned char[1] i.e., char is capable of being represented as an unsigned char. Where am I wrong?

具体的例子,我可以重新present [ - 2,-1,0,1] [0,1,2 3] 。如果我不能,为什么?

Concrete example, I can represent [-2, -1, 0, 1] as [0, 1, 2, 3]. If I can't then why?

相关报道:根据§6.3.1.3 isspace为((unsigned char型)C)是便携式的,如果 INT_MAX&GT; = UCHAR_MAX 否则是实现定义的。

Related: According to §6.3.1.3 isspace((unsigned char)c) is portable if INT_MAX >= UCHAR_MAX otherwise it is implementation-defined.

推荐答案

在该字符的假设是的签署的话,这将是的未定义行为,否则是因为 CHAR_MIN 将具有值 0 。这是比较容易看到的意图和含义:

Under the assumption that char is signed then this would be undefined behavior, otherwise it is well defined since CHAR_MIN would have the value 0. It is easier to see the intention and meaning of:

其值应重新presentable为unsigned char或应
  宏EOF的值相等

the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF

如果我们读一节 7.4 字符处理&LT;&文件ctype.h GT; 的距离的理由国际标准的编程语言-C 它说(的重点煤矿前进

if we read section 7.4 Character handling <ctype.h> from the Rationale for International Standard—Programming Languages—C which says (emphasis mine going forward):

由于这些功能通常主要用作宏,他们的域名
  被限制在一个重新presentable小的正整数
  unsigned char型,加上EOF
的值。 EOF是传统-1,但可能
  是任何负整数,并且从任何有效的,因此可区分
  字符code。这些宏因此,可以通过有效地实施
  使用参数作为索引属性的小型阵列。

Since these functions are often used primarily as macros, their domain is restricted to the small positive integers representable in an unsigned char, plus the value of EOF. EOF is traditionally -1, but may be any negative integer, and hence distinguishable from any valid character code. These macros may thus be efficiently implemented by using the argument as an index into a small array of attributes.

所以,有效值是:


  1. 正整数,可以适应unsigned char型

  2. EOF 这是定义的一些实施负数

  1. Positive integers that can fit into unsigned char
  2. EOF which is some implementation defined negative number

尽管这是C99的理由,因为你是指特定的措辞不从的 C99 来的 C11 等的基本原理仍然适用。

Even though this is C99 rationale since the particular wording you are referring to does not change from C99 to C11 and so the rationale still fits.

我们还可以发现,为什么该接口使用的 INT 的作为,而不是的字符的,从节 7.1.4 的库函数使用的,它说:

We can also find why the interface uses int as an argument as opposed to char, from section 7.1.4 Use of library functions, it says:

所有库原型指定的在加宽类型看
  以前宣布为char参数现在写为int。这个
  的确保大多数库函数可以具有或不具有被称为
  原型范围
,从而保持向后兼容性
  pre-C89 code。但请注意,既然如printf函数和
  scanf函数使用可变长度参数列表,它们必须在被称为
  原型的范围。

All library prototypes are specified in terms of the "widened" types an argument formerly declared as char is now written as int. This ensures that most library functions can be called with or without a prototype in scope, thus maintaining backwards compatibility with pre-C89 code. Note, however, that since functions like printf and scanf use variable-length argument lists, they must be called in the scope of a prototype.

这篇关于&是什么QUOT;再presentable&QUOT;在C11是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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