Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 当我的数据类型是double时,为什么我的输出显示指数结果?_C_Types - Fatal编程技术网

C 当我的数据类型是double时,为什么我的输出显示指数结果?

C 当我的数据类型是double时,为什么我的输出显示指数结果?,c,types,C,Types,我试图找到3位数字的平均值:3.5 9.6 10.2,它们是浮点数 然而,我无法理解为什么我的输出显示出如此奇怪的数字 /* Computes pairwise averages of three numbers */ #include <stdio.h> double average(double a , double b ) { return (a + b)/2 ; } int main (void) { double x, y,z; printf

我试图找到3位数字的平均值:3.5 9.6 10.2,它们是浮点数

然而,我无法理解为什么我的输出显示出如此奇怪的数字

/* Computes pairwise averages of three numbers */

#include <stdio.h>

double average(double a , double b )
{
    return (a + b)/2 ;
}

int main (void)
{
    double x, y,z;

    printf("Enter three numbers: ");
    scanf("%g%g%g", &x,&y,&z);
    printf("Average of %g and %g : %g\n", x,y,average(x,y));
    printf("Average of %g and %g : %g\n", y,z,average(y,z));
    printf("Average of %g and %g : %g\n", x,z,average(x,z));

    return 0;
}
/*计算三个数字的成对平均值*/
#包括
双倍平均(双倍a,双倍b)
{
返回(a+b)/2;
}
内部主(空)
{
双x,y,z;
printf(“输入三个数字:”);
scanf(“%g%g%g”、&x、&y和&z);
printf(“%g和%g的平均值:%g\n”,x,y,平均值(x,y));
printf(“%g和%g的平均值:%g\n”,y,z,平均值(y,z));
printf(“%g和%g的平均值:%g\n”,x,z,平均值(x,z));
返回0;
}

节省宝贵的时间,并允许所有编译器警告捕获这些简单错误

%g
需要匹配的
浮点*

%lg
需要匹配的
双*

double x, y,z;
// scanf("%g%g%g", &x,&y,&z);
scanf("%lg%lg%lg", &x,&y,&z);

更好的代码还会检查
scanf()
返回值

if (scanf("%lg%lg%lg", &x,&y,&z) == 3) Success();

请查看编译器警告。格式字符串“%g”需要类型为“float*”的参数,但变量参数1的类型为“double*”。注意:考虑在格式字符串中使用“%LG”。也请让它测试<代码> SCANFER()/<代码>的结果,如<代码>(Snf)(%g%g%g,and x,y,z)=3){/*处理错误*/}