添加两个字符产生int [英] Addition of two chars produces int

查看:403
本文介绍了添加两个字符产生int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的程序,并用GCC 4.4 / 4.5编译如下:

I've made a simple program and compiled it with GCC 4.4/4.5 as follows:

int main ()
{
  char u = 10;
  char x = 'x';
  char i = u + x;

  return 0;
}

g ++ -c -Wconversion a.cpp

g++ -c -Wconversion a.cpp

我有以下:

a.cpp: In function ‘int main()’:
a.cpp:5:16: warning: conversion to ‘char’ from ‘int’ may alter its value

对于以下代码,我得到了相同的警告:

The same warning I've got for the following code:

  unsigned short u = 10;
  unsigned short x = 0;
  unsigned short i = u + x;

a.cpp: In function ‘int main()’:
a.cpp:5:16: warning: conversion to ‘short unsigned int’ from ‘int’ may alter its value

任何人都可以解释为什么添加两个字符(或两个无符号短整型)
是编译器错误还是符合标准?

Could anyone please explain me why addition of two chars (or two unsigned shorts) produces int? Is it a compiler bug or is it standard compliant?

谢谢。

推荐答案

你看到的是在算术表达式中发生的所谓通常的算术转换的结果,特别是那些二进制的(带两个参数)。

What you're seeing is the result of the so-called "usual arithmetic conversions" that occur during arithmetic expressions, particularly those that are binary in nature (take two arguments).

这在§5/ 9中描述:

This is described in §5/9:


许多希望算术或枚举操作数的二进制运算符类型导致转换和产出结果类型以类似的方式。目的是产生一个共同的类型,这也是结果的类型。这种模式称为通常的算术转换,定义如下:

- 如果任一操作数的类型 long double ,另一个将转换为 long double

- 否则,如果任一操作数 double ,另一个将转换为 double

- 否则,如果任一操作数 float ,另一个将转换为 float

- 否则,积分促销 54)

- 然后,如果任一操作数 unsigned long ,则另一个将转换为 unsigned long

- 否则,如果一个操作数为 long int c $ c> unsigned int ,则如果 long int 可以表示 unsigned int unsigned int 将转换为 long int ;否则两个操作数都应转换为 unsigned long
int


- 否则,如果任一操作数 long ,另一个将转换为 long

- 否则,如果任一操作数 unsigned ,另一个应转换为 unsigned

— If either operand is of type long double, the other shall be converted tolong double.
— Otherwise, if either operand is double, the other shall be converted to double.
— Otherwise, if either operand is float, the other shall be converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on both operands.54)
— Then, if either operand is unsigned long the other shall be converted to unsigned long.
— Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
— Otherwise, if either operand is long, the other shall be converted to long.
— Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

[注意:否则,唯一剩下的情况是两个操作数都是 int ]

[Note: otherwise, the only remaining case is that both operands are int]

§4.5中提及的促销是:

The promotions alluded to in §4.5 are:


1 char signed char unsigned char short int unsigned short int 可以转换为 int if int 可以表示源类型的所有值;否则,源rvalue可以转换为 unsigned int 类型的右值。

1 An rvalue of type char, signed char, unsigned char, short int, or unsigned short intcan be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

2类型 wchar_t (3.9.1)或枚举类型(7.2)的右值可转换为可以表示其底层类型的所有值的第一个类型的右值: int unsigned int long unsigned long

2 An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long, or unsigned long.

3整数位字段(9.6)的右值可以转换为类型 int 的右值if int 可以表示位域的所有值;否则,如果 unsigned int 可以表示位字段的所有值,则可以将其转换为 unsigned int 。如果位字段较大,则不应用积分提升。如果位字段具有枚举类型,则将其视为该类型的任何其他值以用于升级目的。

3 An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.

4类型 bool 的右值可以转换为 int false 变为零, true 变为 c $ c>。

4 An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

5这些转换称为整体促销。

5 These conversions are called integral promotions.

例如乘法运算符添加运算符都包含以下短语:执行常见的算术转换...

From here, sections such as "Multiplicative operators" or "Additive operators" all have the phrase: "The usual arithmetic conversions are performed..." to specify the type of the expression.

换句话说,当你做整数运算时,类型是用上面的类别确定的。在您的情况下,促销由§4.5/ 1涵盖,并且表达式的类型为 int

In other words, when you do integral arithmetic the type is determined with the categories above. In your case, the promotion is covered by §4.5/1 and the type of the expressions are int.

这篇关于添加两个字符产生int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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