scanf to handle double

R

Red Dragon

Can someone please tell me how I should write scanf to handle double?
This program works fine for float a but not double a, as the print result
at end of program illustrates.
Thanks
Khoon

#include <stdio.h>

int main (void)
{
double a;
a =2.1111;

printf ("Enter a> ");
scanf ("%f", &a);
printf ("a=%4.4f\n",a);
return 0;
}

/*
Result:
Enter a> 555.55
a=2.1111
Press any key to continue
*/
 
M

Mike Wahler

Red Dragon said:
Can someone please tell me how I should write scanf to handle double?
This program works fine for float a but not double a, as the print
result at end of program illustrates.
Thanks
Khoon

#include <stdio.h>

int main (void)
{
double a;
a =2.1111;

printf ("Enter a> ");
scanf ("%f", &a);

scanf("%lf", &a);

-Mike
 
O

Old Wolf

Red said:
With your help I learnt something today.

Note that printf still uses "%f" for doubles. Don't try using
"%lf" with printf as it may not work.
 
M

Mike Wahler

Old Wolf said:
Note that printf still uses "%f" for doubles. Don't try using
"%lf" with printf as it may not work.

Yes, this trips up many folks. The lists of 'printf()'
and 'scanf()' format specifiers are very close, but not
exactly the same.

-Mike
 
M

Michael Mair

Mike said:
Yes, this trips up many folks. The lists of 'printf()'
and 'scanf()' format specifiers are very close, but not
exactly the same.

Note: C99 allows "%lf" for doubles.
As this does not concern many people, "%f" of course is the
correct thing.


Cheers
Michael
 
R

Red Dragon

Note that printf still uses "%f" for doubles. Don't try using
"%lf" with printf as it may not work.

Yes. I noticed that. I wonder why the creater of C cannot do the same
for scanf as he has done for printf.
If he can do away with l in printf why cant he do so in scanf?
It would have made life easier for all of us, especially for myself. I got
tripped up earlier.
Thanks
Khoon.
 
M

Michael Mair

Red said:
Yes. I noticed that. I wonder why the creater of C cannot do the same
for scanf as he has done for printf.
If he can do away with l in printf why cant he do so in scanf?
It would have made life easier for all of us, especially for myself. I got
tripped up earlier.

The answer is clearer if you look at what happens:
For scanf(), one passes the address of an object, telling only
via the format specifier plus length modifier what kind of object
one is passing. If float and double have different sizes (or
different representations), then one _cannot_ use "%f" for
both -- if we assume different sizes, then we would only change
the first sizeof (float) bytes of the double object which almost
certainly does not yield the same value. Moreover, this may yield
an invalid double representation.
The other way round: For functions with variable argument lists,
float values are passed as double values. Thus, "%lf" would have
been better than "%f" for printf() but convenience won.

If you want to know more, I recommend looking at C's historical
development.


Cheers
Michael
 
P

Peter Nilsson

Red said:
Yes. I noticed that. I wonder why the creater of C cannot do the same
for scanf as he has done for printf.
If he can do away with l in printf why cant he do so in scanf?

It's because the argument promotions which apply to printf are not
relevant to scanf. [Printf takes floats and converts them to doubles,
but
_pointers_ to floats are not converted to pointers to doubles in scanf
calls.]
It would have made life easier for all of us, especially for myself.

Opening and reading a decent C book/tutorial would make life easier for
you
too. BTW, it's never too late for that. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top