字符类型乘以另一个字符 [英] Type of char multiply by another char

查看:89
本文介绍了字符类型乘以另一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C/C ++中两个字符相乘的结果的类型是什么?

unsigned char a = 70;
unsigned char b = 58;
cout << a*b << endl; // prints 4060, means no overflow
cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means overflow

我希望将类型T的两个数字(例如char,short,int)相乘的结果变成T.对于char,它似乎是int,因为sizeof(a*b)是4.

我编写了一个简单的函数来检查乘法结果的大小:

template<class T>
void print_sizeof_mult(){
  T a;
  T b;
  cout << sizeof(a*b) << endl;
}

print_sizeof_mult<char>()print_sizeof_mult<short>()print_sizeof_mult<int>()是4,print_sizeof_mult<long>()是8.

这些结果是否仅适用于我的特定编译器和计算机体系结构?还是在某处记录了C/C ++基本操作的输出是什么类型?

解决方案

根据C ++标准(4.5 整体促销)

1除bool,char16_t,char32_t或bool以外的整数类型的prvalue wchar_t,其整数转换等级(4.13)小于 如果int可以表示所有整数,则可以将int转换为int类型的prvalue 源类型的值;否则,源prvalue可以是 转换为unsigned int类型的prvalue.

和(5个表达式)

10许多期望算术或算术操作数的二进制运算符 枚举类型原因转换和收益结果类型类似 道路.目的是产生一个通用类型,这也是 结果.这种模式称为通常的算术转换, 定义如下:

....

  • 否则,积分促销(4.5)应在两者上执行 操作数 .61然后,将以下规则应用于被提升的对象 操作数:

最后(5.6个乘法运算符)

2 *和/的操作数应为算术或无作用域 枚举类型; %的操作数应具有整数或无作用域 枚举类型. 通常的算术转换是在 操作数,并确定结果的类型.

类型charshort的转换排名小于类型int的排名.

What is the type of the result of a multiplication of two chars in C/C++?

unsigned char a = 70;
unsigned char b = 58;
cout << a*b << endl; // prints 4060, means no overflow
cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means overflow

I expect the result of multiplying two number of type T (e.g., char, short, int) becomes T. It seems it is int for char because sizeof(a*b) is 4.

I wrote a simple function to check the size of the result of the multiplication:

template<class T>
void print_sizeof_mult(){
  T a;
  T b;
  cout << sizeof(a*b) << endl;
}

print_sizeof_mult<char>(), print_sizeof_mult<short>(), and print_sizeof_mult<int>() are 4 and print_sizeof_mult<long>() is 8.

Are these result only for my particular compiler and machine architecture? Or is it documented somewhere that what type is the output of basic operations in C/C++?

解决方案

According to the C++ Standard (4.5 Integral promotions)

1 A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

and (5 Expressions)

10 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:

....

  • Otherwise, the integral promotions (4.5) shall be performed on both operands.61 Then the following rules shall be applied to the promoted operands:

and at last (5.6 Multiplicative operators)

2 The operands of * and / shall have arithmetic or unscoped enumeration type; the operands of % shall have integral or unscoped enumeration type. The usual arithmetic conversions are performed on the operands and determine the type of the result.

Types char and short have conversion ranks that are less than the rank of the type int.

这篇关于字符类型乘以另一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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