c语言编程出错

来源:百度知道 编辑:UC知道 时间:2024/06/12 16:29:54
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c;
int n;
b=0;
for(n=1;a>=1E-6;n++)
{
a=1.0/(2*n-1);
c=pow(-1,n-1);
b=b+a*c;
}
printf("%f",b);
}

一运行就提示floating point error,想请各位高手指点一下
多谢,我试过了,果然。那是不是所有的数值变量在使用前都要赋初值呢?

你在进行循环之前,还没给a赋值呢

A没有初值,他的值就会任取,

void main()
{
double a=1,b=0,c=-1;
int n;
b=0;
for(n=1;a>=1E-6;n++)
{
a=1.0/(2*n-1);
c=(-1)*c
b=b+a*c;
}
printf("%lf",b);
}