首页 > 解决方案 > 当浮点变量除以c ++中的整数变量时的结果类型

问题描述

我在 geeksforgeeks.com 上用 c++ 解决了一个问题,我在其中确定了操作后变量的结果类型。

输入:1 1 2 3 5 gfgc

输出:4 8 4 8 32 1

Example:
Testcase 1:
b/c = 2/3 =>sizeof(2/3)=>float size is 4 bytes

"b/a = 2/1 =>sizeof(2/1)=>double size is 8 bytes"

c/a = 3/1 =>sizeof(3/1)=>integer size is 4 bytes
(c/a)+l =3+5= 8 =>sizeof(8)=>long long size is 8 bytes
sizeof(gfgc) = 32 => It is not 4 because of the reason listed here
sizeof(c) = 1 as it is just a character.

有人可以解释引号中显示的输出背后的原因吗?

标签: c++

解决方案


给定float aand int b(反之亦然)和表达式auto c = a/b两者ab都被强制转换为floats 以进行除法,并且c是 afloat

更一般地,如果任一操作数是浮点类型,则生成的类型将在此列表中的第一个表达式中找到:long doubledoublefloat; 另一个操作数转换为该类型。如果两个操作数都是整数,则适用类似的规则。

这些是“通常的算术转换”。请参阅[expr.arith.conv]


推荐阅读