Addition of two chars produces int(两个字符相加产生 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
谁能解释一下为什么添加两个字符(或两个无符号短裤)会产生 int?是编译器错误还是符合标准?
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:
许多期望算术或枚举类型的操作数的二元运算符会导致转换并以类似的方式产生结果类型.目的是产生一个通用类型,这也是结果的类型.这种模式称为常用算术转换,定义如下:
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:
——如果任一操作数的类型为long double
,则另一个应转换为long double
.
— 否则,如果任一操作数为 double
,则另一个应转换为 double
.
— 否则,如果任一操作数为 float
,则另一个应转换为 float
.
— 否则,应在两个操作数上执行积分提升 (4.5).54)
— 然后,如果任一操作数是 unsigned long
另一个应转换为 unsigned long
.
— 否则,如果一个操作数是一个 long int
而另一个是 unsigned int
,那么如果一个 long int
可以表示一个 unsigned int
,unsigned int
应转换为 long int
;否则两个操作数都应转换为 unsigned long整数
.
— 否则,如果任一操作数为 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
可以表示源类型的所有值,则可以转换为 int
类型的右值;否则,源右值可以转换为 unsigned int
类型的右值.
1 An rvalue of type
char
,signed char
,unsigned char
,short int
, orunsigned short int
can be converted to an rvalue of typeint
ifint
can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of typeunsigned 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 如果 int
可以表示位域的所有值,则整数位域 (9.6) 的右值可以转换为 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
变为 0 并且 true
变成 one
.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:两个字符相加产生 int
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01