两个字符相加产生 int
我制作了一个简单的程序,并使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所看到的是算术表达式期间发生的所谓“常规算术转换”的结果,特别是那些本质上是二进制的表达式(采用两个参数)。
§5/9 对此进行了描述:
§4.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:
The promotions alluded to in §4.5 are:
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
.当对
char
类型进行算术运算时,返回的结果是int
类型。看这个:
输出:
ideone 演示:http://www.ideone.com/jNTMm
When you do any arithmetic operation on
char
type, the result it returns is ofint
type.See this:
Output:
Demonstration at ideone : http://www.ideone.com/jNTMm
当您将这两个字符相加时,它们首先会被提升为 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