如何在c中划分2 int?

Ale*_*lex 4 c division

想要划分2个数字,得到如下结果:

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);
Run Code Online (Sandbox Code Playgroud)

Har*_*ris 13

您需要一个double变量来存储结果.int只存储整数.此外,您还必须在执行除法之前对其他变量进行类型转换.


做这样的事情

double c;
.
.
.
c = (double)a / (double)b;
printf("%f", c);
Run Code Online (Sandbox Code Playgroud)

注意:

您不需要&in printf()语句.