Printf 产生错误的整数
Printf produces wrong integers
我学了几天C,用Printf转换温度时显示错误。
int main() {
char sys;
int temp;
printf("Enter a tempature system [c or f]: ");
scanf("%c", &sys);
printf("Enter a tempature: ");
scanf("%s", &temp);
if (sys == 'f') {
int output = (temp - 32) * 5/9;
printf("%d Fahrenheit is %d Celsius\n",temp,output);
return 0;
}
else if (sys == 'c') {
int output = temp * 9/5 + 32;
printf("%d Celsius is %d Fahrenheit\n",temp,output);
return 0;
}
}
#include <stdio.h>
int main() {
char sys;
float temp,output;
printf("Enter a tempature system [c or f]: ");
scanf("%c", &sys);
printf("Enter a tempature: ");
scanf("%f", &temp);
if (sys == 'f') {
output = (temp - 32.0) * (5.0 / 9.0);
printf("%.2f Fahrenheit is %.2f Celsius\n",temp,output);
}
else if (sys == 'c') {
output = (temp * 9.0 / 5.0) + 32.0;
printf("%.2f Celsius is %.2f Fahrenheit\n",temp,output);
}
return 0;
}
scanf("%s", &temp);
%s
格式说明符用于字符串。您正在读取一个整数,所以您需要 %d
.
我学了几天C,用Printf转换温度时显示错误。
int main() {
char sys;
int temp;
printf("Enter a tempature system [c or f]: ");
scanf("%c", &sys);
printf("Enter a tempature: ");
scanf("%s", &temp);
if (sys == 'f') {
int output = (temp - 32) * 5/9;
printf("%d Fahrenheit is %d Celsius\n",temp,output);
return 0;
}
else if (sys == 'c') {
int output = temp * 9/5 + 32;
printf("%d Celsius is %d Fahrenheit\n",temp,output);
return 0;
}
}
#include <stdio.h>
int main() {
char sys;
float temp,output;
printf("Enter a tempature system [c or f]: ");
scanf("%c", &sys);
printf("Enter a tempature: ");
scanf("%f", &temp);
if (sys == 'f') {
output = (temp - 32.0) * (5.0 / 9.0);
printf("%.2f Fahrenheit is %.2f Celsius\n",temp,output);
}
else if (sys == 'c') {
output = (temp * 9.0 / 5.0) + 32.0;
printf("%.2f Celsius is %.2f Fahrenheit\n",temp,output);
}
return 0;
}
scanf("%s", &temp);
%s
格式说明符用于字符串。您正在读取一个整数,所以您需要 %d
.