两个字符相加产生 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 functionint main()’:
a.cpp:5:16: warning: conversion tocharfromint’ 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 functionint main()’:
a.cpp:5:16: warning: conversion to ‘short unsigned intfromint’ 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 intunsigned 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 charsigned charunsigned charshort int 类型的右值>unsigned short int如果 int 可以表示源类型的所有值,则可以转换为 int 类型的右值;否则,源右值可以转换为 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) 类型的右值可以转换为以下第一种类型的右值,可以表示其底层的所有值类型:intunsigned intlongunsigned 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1158

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1059

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:798

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:884

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:870

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:854

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1688

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1294