当两个都是变量时,如何将浮点数除以整数? [英] How to divide float by integer when both are variables?

查看:290
本文介绍了当两个都是变量时,如何将浮点数除以整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编写程序.我有两个变量,其中一个是整数,另一个是浮点数.我想将浮点数除以整数,并想获得浮点数的结果.我正在执行以下操作:

I am writing a program in C. I have two variables one of which is integer and the other one is float. I want to divide the float by the integer and want to get result in float. I am doing the following:

int a;
float b=0,c=0;
scanf("%d",&a);

然后我对b进行一些计算,使其具有非零值.

I then do some computations on b so it has a non-zero value.

c = b/(float)a;
printf("c = %d\n", c);

问题是我将c打印为四舍五入的数字(整数),而不是浮点数.

The problem is I am getting c printed as a rounded number (integer) rather than a float value.

如何获取c作为浮点值?

推荐答案

您的问题在这里:

printf("c = %d\n", c);

%d将c格式化为整数.改为使用%f.

%d formats c as integer. Use %f instead.

std::cout << "c = " << c << std::endl(如果您愿意).

这篇关于当两个都是变量时,如何将浮点数除以整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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