c语言关于类型转换的问题,编译的时候会出现下面两个警告,应该怎么消除这两个警告?
#include<stdio.h>#defineCPI2.54//centimetersperinches#defineIPF12//inchesperfeetintma...
#include <stdio.h>
#define CPI 2.54 //centimeters per inches
#define IPF 12 //inches per feet
int main(void)
{
int feet;
float centimeters, inches;
printf("Enter a height in centimeters:");
while (scanf("%f", ¢imeters) == 1 && centimeters > 0)
{
feet = centimeters / (CPI * IPF);
inches = centimeters / CPI - feet * IPF;
printf("%.1f cm = %d feet, %.1f inches\n", centimeters, feet, inches);
printf("Enter a height in centimeters (<=0 to quit):");
}
printf("Bye!");
return 0;
}
E:\2.c(14) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data
E:\2.c(15) : warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data 展开
#define CPI 2.54 //centimeters per inches
#define IPF 12 //inches per feet
int main(void)
{
int feet;
float centimeters, inches;
printf("Enter a height in centimeters:");
while (scanf("%f", ¢imeters) == 1 && centimeters > 0)
{
feet = centimeters / (CPI * IPF);
inches = centimeters / CPI - feet * IPF;
printf("%.1f cm = %d feet, %.1f inches\n", centimeters, feet, inches);
printf("Enter a height in centimeters (<=0 to quit):");
}
printf("Bye!");
return 0;
}
E:\2.c(14) : warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data
E:\2.c(15) : warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data 展开
3个回答
展开全部
一、
int feet;
float centimeters, inches;
改成
double feet ,centimeters, inches;
然后所有的%d %f改成%lf
或
二、
feet = centimeters / (CPI * IPF);
inches = centimeters / CPI - feet * IPF;
改成:
feet = (int)(centimeters / (CPI * IPF));
inches = (float)(centimeters / CPI - feet * IPF);
其实这是编译器的问题,且是警告,不处理也行
int feet;
float centimeters, inches;
改成
double feet ,centimeters, inches;
然后所有的%d %f改成%lf
或
二、
feet = centimeters / (CPI * IPF);
inches = centimeters / CPI - feet * IPF;
改成:
feet = (int)(centimeters / (CPI * IPF));
inches = (float)(centimeters / CPI - feet * IPF);
其实这是编译器的问题,且是警告,不处理也行
展开全部
int feet;
float centimeters, inches;
修改为
double feet;
double centimeters, inches;
然后将输出都用 %lf输出
float centimeters, inches;
修改为
double feet;
double centimeters, inches;
然后将输出都用 %lf输出
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#define CPI 2.54 //centimeters per inches
#define IPF 12 //inches per feet
int main(void)
{
double feet, inches;
float centimeters;
printf("Enter a height in centimeters:");
while (scanf("%f", ¢imeters) == 1 && centimeters > 0)
{
feet = centimeters / (CPI * IPF);
inches = centimeters / CPI - feet * IPF;
printf("%.1f cm = %d feet, %.1f inches\n", centimeters, feet, inches);
printf("Enter a height in centimeters (<=0 to quit):");
}
printf("Bye!");
return 0;
}
#define CPI 2.54 //centimeters per inches
#define IPF 12 //inches per feet
int main(void)
{
double feet, inches;
float centimeters;
printf("Enter a height in centimeters:");
while (scanf("%f", ¢imeters) == 1 && centimeters > 0)
{
feet = centimeters / (CPI * IPF);
inches = centimeters / CPI - feet * IPF;
printf("%.1f cm = %d feet, %.1f inches\n", centimeters, feet, inches);
printf("Enter a height in centimeters (<=0 to quit):");
}
printf("Bye!");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询