scanf("lf",&r);
Should be
scanf("%lf",&r);
Question
I have the following code:
int main(void)
{
double r;
scanf("lf",&r);
printf("%lf\n",r);
}
Any value I enter returns a nonsense-number with about 40-50 digits. scanf works fine with chars, integers, strings etc., but with doubles I get this problem. Can anybody help me?
Solution
scanf("lf",&r);
Should be
scanf("%lf",&r);
OTHER TIPS
You're missing the percent (%
) symbol in the format string, it should be:
scanf("%lf", &r);
and then you should add checking the return value since, as you just learned, I/O is brittle and can fail. Thus, it's wrong to depend on r
containing a valid value unless you've verified that the scanning worked:
if(scanf("%lf", &r) == 1)
{
print("got the double %g\n", r);
}
else
{
printf("**Failed to read a double value!\n");
}
You need %lf
when you read it in.
Try
scanf("%lf",&r);
You forgot to put in the %
.
You need
scanf("%lf", &r);
Anytime you use scanf() you need to put the percent sign in front of what type of variable you are using.
You forgot %
before lf
:
scanf("%lf", &yourDouble);