如何在c中划分2 int? [英] How to divide 2 int in c?

查看:104
本文介绍了如何在c中划分2 int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要除以2个数字并得到如下结果:

wanna divide 2 numbers and get the result like this:

5/2 = 2.50

5 / 2 = 2.50

但是它只输出2。

我现在不做错什么。

这里是我的代码:

int a;
int b;
int c;
printf("First num\n");
scanf("%d", &a);
printf("Second num\n");
scanf("%d", &b);
c = a / b;
printf("%d", c);


推荐答案

您需要 double 变量来存储结果。 int 仅存储整数。此外,在执行除法运算之前,还必须转换其他变量。

You need a double variable to store the result. int stores only integers. Additionally, you have to typecast the other variables also before performing the division.

执行类似的操作

double c;
.
.
.
c = (double)a / (double)b;
printf("%f", c);






注意:

您不需要 printf()& >语句。

You do not need the & in printf() statements.

这篇关于如何在c中划分2 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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