C语言double输入
程序1:
#include<stdio.h>
#include<math.h>
void main()
{
double x1, x2, y1, y2;
scanf_s("%f%f%f%f", &x1, &y1, &x2, &y2);
printf("%.2f\n", sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
}
程序2:
#include<stdio.h>
#include<math.h>
void main()
{
float x1, x2, y1, y2;
scanf_s("%f%f%f%f", &x1, &y1, &x2, &y2);
printf("%.2f\n", sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
}
只把一个double变成了float,输出结果却大相径庭
程序1输入:1 5 3 7
输出:151443012912120570646056432312962929087987323884273664.00
程序2输入:1 5 3 7
输出:2.83
深究原因,原来double输入不能用%f,而要用%lf
程序3:
#include<stdio.h>
#include<math.h>
void main()
{
double x1, x2, y1, y2;
scanf_s("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
printf("%.2f\n", sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
}
程序3输入:1 5 3 7
输出:2.83