其中积分促销打印一个char什么时候发生? [英] Which integral promotions do take place when printing a char?

查看:136
本文介绍了其中积分促销打印一个char什么时候发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我读了

unsigned char x=1;
printf("%u",x);

调用不确定的行为,因为由于格式说明%U,预计的printf一个unsigned int。但我仍想了解什么是在这个例子中事。

invokes undefined behaviour since due to the format specifier %u, printf expects an unsigned int. But still I would like to understand what is going on in this example.

我认为,整体推进的规则适用于除权pression 的printf(%U,X)
X psented转口货值为$ P $。

I think that the integral promotion rules apply with the expression printf("%u",x) and the value represented by x.

A.6.1积分促销

一个字符,短整数或整数位字段,所有有符号或没有,或一个
  枚举类型的对象,可能在一个前pression用于任何可以使用的整数。
  如果int可以重新present原始类型的所有值,则该值转换
  为int;否则,值转换为unsigned int类型。这个过程被称为积分
  推广。

A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer may be used. If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion.

可以使用是什么意思呢在这里?这是否意味着语法正确或定义的行为?

What does "may be used" mean here? Does it mean 'is syntactically correct' or 'is defined behaviour'?

和如何为x在这个例子中提升?我已阅读,它被提升到一个int,但如果的printf(%U(INT X))仍然不确定的行为那么我真的不明白为什么...

And how is x promoted in this example? I have read that it is promoted to an int, but if printf("%u", (int x)) is still undefined behaviour then I don't really understand why...

推荐答案

由于的printf 使用可变参数列表,整数促销应用到它的整数参数。在任何正常的C语言的实现,整数转换成促销的 unsigned char型 INT 。然后要格式化的 INT 随着 unsigned int类型的说明符,这样的行为是不确定的。

Since printf uses a variable argument list, the integer promotions are applied to its integer arguments. In any normal C implementation, the integer promotions convert an unsigned char to an int. Then you are formatting an int With a specifier for unsigned int, so the behavior is undefined.

有是说,一个角色可以使用的地方,可以使用一个整数,你的语句不是由C标准定义的行为事实之间不存在冲突。虽然你可以代替整数使用字符​​,关于什么可以与%U 打印的规则仍然适用。如果使用适当的说明整数一个字符的结果,其行为定义。如果使用在不合适的说明符的整数的字符结果中,该行为没有被C标准定义

There is no conflict between saying that a character may be used where an integer may be used and the fact that your statement has behavior not defined by the C standard. Although you may use a character in place of an integer, the rules about what may be printed with %u still apply. If using a character results in an integer appropriate for the specifier, the behavior is defined. If using a character results in an integer inappropriate for the specifier, the behavior is not defined by the C standard.

浅谈堆栈溢出其他地方的结论是异国情调的C实现的可能在理论上符合C标准,同时具有字符类型(普通纸,签署,和无符号)一样宽 INT 类型。在这样的实施,而 INT 不能重新present一个 unsigned char型的所有值,所以一个 unsigned char型将不得不提升到 unsigned int类型。然而,这样的实现将是异国情调和麻烦(尤其是与处理EOF),你可能会忽略它在实践中。

Discussion elsewhere on Stack Overflow concluded that an exotic C implementation might in theory conform to the C standard while having char types (plain, signed, and unsigned) as wide as int types. In such an implementation, and int could not represent all values of an unsigned char, so an unsigned char would have to be promoted to an unsigned int. However, such an implementation would be exotic and troublesome (notably with handling EOF), and you may ignore it in practice.

这篇关于其中积分促销打印一个char什么时候发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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