float变量的输出为0.0000 [英] The output for float variable is 0.0000

查看:117
本文介绍了float变量的输出为0.0000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#包括< stdio.h中> 
#include< math.h>
main()
{
浮动a,b,c,d;
printf(输入& b\ n的值);
scanf(%f%f,& a,& b);
c = a + b;
d = a * b;
printf(总和是%f \ n,c);
printf(乘法是%f \ n,d);
return(0);
}





我的尝试:



我尝试使用math.h作为预处理器。请看看我错过了什么,因为和和乘法的输出都是0.0000。

解决方案

简单:你的双引号位置错误。

改变这个:

 scanf(%f%f,& a,& b); 

对此:

 scanf(%f%f,& a,& b); 


使用调试器时,您可能会发现错误。它在scanf中。正确的是:

 scanf( %f%f ,& a,& b); 

阅读 scanf文档了解你的错误。


也许你想删除程序中的引文,

 scanf( %f%f,& a,& b); 



做到这样,

 scanf( %f%f ,& a,& b); 



这将保存变量中的值,并允许程序进一步计算值并打印它们。另外,请遵循语言标准并使用

  int  main() / *  甚至是main(int argc,char ** argv)* /  



所以现在程序会最终是这样的,

  #include   <   stdio.h  >  
#include < math.h >
int main()< span class =code-comment> // 好方法,否则编译器会这样做。
{
float a,b,c,d;
printf( 输入& b\ n的值);
scanf( %f%f,& a,& b); // < - 见这里。
c = a + b;
d = a * b;
printf( 总和为%f \ n,c);
printf( 乘法是%f \ n,d);
return 0 );
}



这个程序的输出是:

 1 
2
输入&的值。 b
总和是3.000000
乘法是2.000000



C,这并不难。 : - )


#include<stdio.h>
#include<math.h>
main()
{
	float a,b,c,d;
	printf("Enter the value for a & b\n");
	scanf("%f %f, &a, &b");
	c=a+b;
	d=a*b;
	printf("The sum is %f\n", c);
	printf("the Multiplication is %f\n", d);
	return (0);
}



What I have tried:

I tried using the math.h as pre-processor. Please have a look what am I missing due to which the output is 0.0000 for both sum and multiplication.

解决方案

Simple: your double quotes are in the wrong place.
Change this:

scanf("%f %f, &a, &b");

To this:

scanf("%f %f", &a, &b);


When you use the debugger you may find the bug. It is in the scanf. Correct is:

scanf("%f %f", &a, &b);

Read the documentation of scanf to understand your bug.


Maybe you want to remove the quotations from the program,

scanf("%f %f, &a, &b");


Make it something like this,

scanf("%f %f", &a, &b);


This will now save the values in the variables and allow the program to compute the values further down and print them as well. Also, please follow the standards of the language and use

int main() /* even main(int argc, char** argv) */


So now the program would end up being something like this,

#include<stdio.h>
#include<math.h>
int main() // Good approach, otherwise compiler will do it.
{
	float a, b, c, d;
	printf("Enter the value for a & b\n");
	scanf("%f %f", &a, &b); // <-- see here.
	c = a + b;
	d = a * b;
	printf("The sum is %f\n", c);
	printf("the Multiplication is %f\n", d);
	return (0);
}


The output of this program is:

1
2
Enter the value for a & b
The sum is 3.000000
the Multiplication is 2.000000


C, this is not tough. :-)


这篇关于float变量的输出为0.0000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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