Alex Rivera | Logout

Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Asked 2008-10-16T23:05:15.953
197

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?

Example code:

double d;
scanf("%lf", &d);
printf("%f", d);
Edit
Report

1 Answer

7

Because otherwise scanf will think you are passing a pointer to a float which is a smaller size than a double, and it will return an incorrect value.

answered 2008-10-16T23:10:28.097

Your Answer