我正在尝试在 C 中运行一些基本代码来声明 2 个浮点变量,然后将它们相除并将该值放入第三个变量中。之后我打印所有 3 个。
#include <stdio.h>
int main ()
{
/* variable definition: */
float a, b, c;
/* variable initialization */
a = 1.2;
b = 2.7;
c = a / b;
printf("Floats (a,b) and quotient (c) are : %d,%d,%d \n", a,b,c);
return 0;
}
我使用在线编译器“www.ideone.com”来编译并运行代码,这是我得到的结果:
Success time: 0 memory: 2156 signal:0
float (a,b) 和商 (c) 为:1073741824,1072902963,-1610612736
谁能看看我是否在代码中犯了错误?这是一个类,在我从 int 更改为 float 之前,每一步的指示一切都运行良好。
最佳答案
您想要打印 float ,因此更改此:
printf("Floats (a,b) and quotient (c) are : %d,%d,%d \n", a,b,c);
对此:
printf("Floats (a,b) and quotient (c) are : %f,%f,%f \n", a,b,c);
有关更多信息,请查看ref .
关于c - C 中使用 float 和 printf 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250958/