C程序-Double返回零

C程序-Double返回零,c,double,scanf,C,Double,Scanf,这是C程序 #include <stdio.h> #include <stdlib.h> int main (void){ double voltage, resistance; printf("Enter your Voltage: "); scanf("%f", &voltage); printf("Enter your Resistance: "); scanf("%f", &resistance);

这是C程序

#include <stdio.h>
#include <stdlib.h>

int main (void){

    double voltage, resistance;
    printf("Enter your Voltage: ");
    scanf("%f", &voltage);
    printf("Enter your Resistance: ");
    scanf("%f", &resistance);
    double amps = voltage / resistance;
    printf("With voltage %f and Ohm %fohms, amps is equal to %f \n", voltage, resistance, amps);
    return 0;
}
#包括
#包括
内部主(空){
双电压,电阻;
printf(“输入电压:”);
扫描频率(“%f”和电压);
printf(“输入您的阻力:”);
扫描频率(“%f”和电阻);
双安培=电压/电阻;
printf(“电压为%f,欧姆为%fohms,安培等于%f\n”,电压、电阻、安培);
返回0;
}

为什么电压和电阻变量返回的值为0.00

您应该在scanf中使用
%lf

这对我来说很好:

#include <stdio.h>
#include <stdlib.h>

int main (void){

double voltage, resistance;
printf("Enter your Voltage: ");
scanf("%lf", &voltage);
printf("Enter your Resistance: ");
scanf("%lf", &resistance);
double amps = voltage / resistance;
printf("With voltage %f and Ohm %fohms, amps is equal to %f \n", voltage, resistance, amps);
return 0;
}
#包括
#包括
内部主(空){
双电压,电阻;
printf(“输入电压:”);
扫描频率(“%lf”和电压);
printf(“输入您的阻力:”);
扫描频率(“%lf”、&电阻);
双安培=电压/电阻;
printf(“电压为%f,欧姆为%fohms,安培等于%f\n”,电压、电阻、安培);
返回0;
}

您应该在扫描中使用
%lf

这对我来说很好:

#include <stdio.h>
#include <stdlib.h>

int main (void){

double voltage, resistance;
printf("Enter your Voltage: ");
scanf("%lf", &voltage);
printf("Enter your Resistance: ");
scanf("%lf", &resistance);
double amps = voltage / resistance;
printf("With voltage %f and Ohm %fohms, amps is equal to %f \n", voltage, resistance, amps);
return 0;
}
#包括
#包括
内部主(空){
双电压,电阻;
printf(“输入电压:”);
扫描频率(“%lf”和电压);
printf(“输入您的阻力:”);
扫描频率(“%lf”、&电阻);
双安培=电压/电阻;
printf(“电压为%f,欧姆为%fohms,安培等于%f\n”,电压、电阻、安培);
返回0;
}

对于格式
%f“
表示
浮点值
,要获得
双精度
,您需要一个“long”
浮点值
,其格式前缀为
%l
对于格式
%f“
表示
浮点值
,要获得
双精度
,您需要一个“long”
float
具有格式前缀
l
的,如
%lf

嘿,谢谢你的工作,你能解释一下添加“l”的作用吗?你可能想在
scanf()上RTFM
:你的编译器应该警告格式说明符和传递的参数类型不匹配。如果没有,则增加其警告级别。Take warnings severy.OT:结果字符串缺少电压和电流的单位,输入提示也没有。电阻的单位不是“欧姆”,而是“欧姆”,简称Ω。嘿,多亏了这一点,你能解释一下添加“l”的作用吗?你可能想在
scanf()
:你的编译器应该警告格式说明符和传递的参数类型不匹配。如果没有,则增加其警告级别。Take warnings severy.OT:结果字符串缺少电压和电流的单位,输入提示也没有。电阻单位不是“欧姆”,而是“欧姆”,短Ω。