两个字符相加产生 int

发布于 2024-10-14 16:21:01 字数 659 浏览 2 评论 0原文

我制作了一个简单的程序,并使用 GCC 4.4/4.5 对其进行了编译,如下所示:

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

  return 0;
}

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

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

  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

谁能解释一下我为什么两个字符(或两个无符号短裤)相加会产生 int ? 这是编译器错误还是符合标准?

谢谢。

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

And I've got the following:

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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等数载,海棠开 2024-10-21 16:21:01

您所看到的是算术表达式期间发生的所谓“常规算术转换”的结果,特别是那些本质上是二进制的表达式(采用两个参数)。

§5/9 对此进行了描述:

许多期望算术或枚举类型操作数的二元运算符会以类似的方式导致转换并产生结果类型。目的是产生一个通用类型,这也是结果的类型。这种模式称为通常的算术转换,其定义如下:

— 如果任一操作数的类型为 long double,则另一个操作数应转换为long double
— 否则,如果任一操作数为 double,则另一个操作数应转换为 double
— 否则,如果任一操作数为 float,则另一个操作数应转换为 float
— 否则,将对两个操作数执行积分提升 (4.5)。54)
— 然后,如果任一操作数为 unsigned long,则另一个操作数应转换为 unsigned long
— 否则,如果一个操作数是 long int 且另一个操作数是 unsigned int,则如果 long int 可以表示 long int 的所有值code>unsigned int,unsigned int 应转换为 long int;否则两个操作数都应转换为unsigned long
整数

— 否则,如果任一操作数为 long,则另一个操作数应转换为 long
— 否则,如果任一操作数为无符号,则另一个操作数应转换为无符号

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

§4.5 中提到的提升是:

1 charsigned charunsigned charshort int类型的右值如果int可以表示源类型的所有值,>unsigned Short int可以转换为int类型的右值;否则,源右值可以转换为 unsigned int 类型的右值。

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

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

4 bool 类型的右值可以转换为 int 类型的右值,其中 false 变为零,true< /code> 成为一个

5 这些转化称为积分促销。

从这里开始,“乘法运算符”或“加法运算符”等部分都包含短语:“执行通常的算术转换...”指定表达式的类型。

换句话说,当您进行积分运算时,类型是由上述类别确定的。在您的情况下,促销活动包含在 §4.5/1 中,并且表达式的类型为 int

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).

This is described in §5/9:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

— 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.

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

The promotions alluded to in §4.5 are:

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 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 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 An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

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.

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.

靑春怀旧 2024-10-21 16:21:01

当对 char 类型进行算术运算时,返回的结果是 int 类型。

看这个:

char c = 'A';
cout << sizeof(c) << endl;
cout << sizeof(+c) << endl;
cout << sizeof(-c) << endl;
cout << sizeof(c-c) << endl;
cout << sizeof(c+c) << endl;

输出:

1
4
4
4
4

ideone 演示:http://www.ideone.com/jNTMm

When you do any arithmetic operation on char type, the result it returns is of int type.

See this:

char c = 'A';
cout << sizeof(c) << endl;
cout << sizeof(+c) << endl;
cout << sizeof(-c) << endl;
cout << sizeof(c-c) << endl;
cout << sizeof(c+c) << endl;

Output:

1
4
4
4
4

Demonstration at ideone : http://www.ideone.com/jNTMm

尘世孤行 2024-10-21 16:21:01

当您将这两个字符相加时,它们首先会被提升为 int。

加法的结果是一个右值,它被隐式提升为
如果需要,并且 int 可以包含结果值,请键入 int。
在任何 sizeof(int) > 的平台上都是如此。大小(字符)。
但要注意 char 可能会被视为有符号的 char
这些链接

可以提供进一步的帮助 - wiki安全编码

when you are adding these two characters with each other they are first being promoted to int.

The result of an addition is an rvalue which is implicitly promoted to
type int if necessary, and if an int can contain the resulting value.
This is true on any platform where sizeof(int) > sizeof(char).
But beware of the fact that char might be treated as signed char by
your compiler.

These links can be of further help - wiki and securecoding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文