为什么给我输出 2 我使用类型转换?
Why does give me output 2 where i'm used type cast?
我正在尝试使用以下代码键入 cast int 以浮动。我在使用类型转换之前工作。但这行不通。请帮帮我。
int n;
n=5;
n=(float)n/2;
cout<<n<<endl;
"我预计 5/2 的输出是 2.5,但实际输出是 2。">
我希望 5/2 的输出是 2.5,但实际输出是 2 ?
为了获得2.5
结果n
应该float
类型而不是int
类型,整数不能像2.5
那样容纳浮点数。
试试这个版本
int n = 5;
float res = (float)n/2;
并打印res
参数 n 是一个整数。因此,您尝试为其分配一个浮点数,但他是一个整数。您应该使用另一个类型为 float 的变量或打印它。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 逐位操作的隐式类型转换
- 模板中的类型转换
- 在 C++(和 C)中进行类型转换时明显不一致
- 字符类型转换不兼容
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- C++:用户定义的显式类型转换函数错误
- 为什么在这种情况下,bool 类型的输出等于 0?
- 将类指针类型转换为键时出错
- 通过引用传递参数时C++类型转换
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- C++显式类型转换(C 样式强制转换)的强制表示法和static_cast的多种解释
- C++无效的函数类型转换
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 为什么给我输出 2 我使用类型转换?
- GCC 编译器对类型转换有什么作用?为什么 mac 和 Linux 上的输出不同
- 当我对输出进行类型转换时,有一个有趣的结果