Velocity Reviews > weirdness of scanf

# weirdness of scanf

Kevin Zhou
Guest
Posts: n/a

 05-16-2004
#include <stdio.h>

main()
{
double a;
scanf("%f", &a);
printf("%f",a);
}
the output is always -1.998295 no matter what value i put in

but

#include <stdio.h>

main()
{
int a;
scanf("%d", &a);
printf("%d",a);
}

works wine

Kevin Zhou
Guest
Posts: n/a

 05-16-2004
Kevin Zhou wrote:
> #include <stdio.h>
>
> main()
> {
> double a;
> scanf("%f", &a);
> printf("%f",a);
> }
> the output is always -1.998295 no matter what value i put in
>
> but
>
> #include <stdio.h>
>
> main()
> {
> int a;
> scanf("%d", &a);
> printf("%d",a);
> }
>
> works wine

problem solved
should have used scanf("%lf",&a)
in K&R lf looks like 1f and it doesn't explain why

Leor Zolman
Guest
Posts: n/a

 05-16-2004
On Sat, 15 May 2004 22:19:54 -0400, Kevin Zhou <(E-Mail Removed)> wrote:

>Kevin Zhou wrote:
>> #include <stdio.h>
>>
>> main()
>> {
>> double a;
>> scanf("%f", &a);
>> printf("%f",a);
>> }
>> the output is always -1.998295 no matter what value i put in
>>
>> but
>>
>> #include <stdio.h>
>>
>> main()
>> {
>> int a;
>> scanf("%d", &a);
>> printf("%d",a);
>> }
>>
>> works wine

>
>problem solved
>should have used scanf("%lf",&a)
>in K&R lf looks like 1f and it doesn't explain why

So, do you understand why, now? There /must/ be two different format
conversions for float and double, because the types have different storage
requirements (at least on most systems), and scanf has to know exactly how
wide the object is it's modifying.

The reason this may come as a surprise to beginners is that the
printf-family doesn't distinguish those types. But that's because floats
get promoted to doubles automatically during the call, so the
printf-family functions still see doubles, even when the caller writes
arguments of type float in the argument list.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html

Martin Ambuhl
Guest
Posts: n/a

 05-16-2004
Kevin Zhou wrote:
> #include <stdio.h>
>
> main()

^
The return type of main is int; you should say so. When you get a C99
compiler, you will be *required* to say so.

> {
> double a;
> scanf("%f", &a);

^^
The scanf specifier for double is "%lf".

> printf("%f",a);

^^^
Without an end-of-line character terminating the last line of output,
the behavior of your program is not predictable across platforms.
> }
> the output is always -1.998295 no matter what value i put in

Try this version instead:

#include <stdio.h>

int main(void)
{
double a;
scanf("%lf", &a);
printf("%f\n", a);
return 0;
}

Karthik
Guest
Posts: n/a

 05-16-2004
Kevin Zhou wrote:

> #include <stdio.h>
>
> main()
> {
> double a;
> scanf("%f", &a);

scanf("%lf", &a);

> printf("%f",a);

scanf("%lf", &a);

should fix the bug.

> }
> the output is always -1.998295 no matter what value i put in
>
> but
>
> #include <stdio.h>
>
> main()
> {
> int a;
> scanf("%d", &a);
> printf("%d",a);
> }
>
> works wine

--
Karthik.
Humans please 'removeme_' for my real email.

Dan Pop
Guest
Posts: n/a

 05-17-2004
In <(E-Mail Removed)> Leor Zolman <(E-Mail Removed)> writes:

>The reason this may come as a surprise to beginners is that the
>printf-family doesn't distinguish those types. But that's because floats
>get promoted to doubles automatically during the call, so the
>printf-family functions still see doubles, even when the caller writes
>arguments of type float in the argument list.

Actually, most beginners who get it wrong expect printf to use %lf for
doubles. The mistake was popular enough for C99 to actually bless it and
%lf in printf is no longer undefined behaviour, as it was in C89.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)

Leor Zolman
Guest
Posts: n/a

 05-17-2004
On 17 May 2004 15:46:26 GMT, (E-Mail Removed) (Dan Pop) wrote:

>In <(E-Mail Removed)> Leor Zolman <(E-Mail Removed)> writes:
>
>>The reason this may come as a surprise to beginners is that the
>>printf-family doesn't distinguish those types. But that's because floats
>>get promoted to doubles automatically during the call, so the
>>printf-family functions still see doubles, even when the caller writes
>>arguments of type float in the argument list.

>
>Actually, most beginners who get it wrong expect printf to use %lf for
>doubles. The mistake was popular enough for C99 to actually bless it and
>%lf in printf is no longer undefined behaviour, as it was in C89.

Cool. I didn't know that. In beginning C classes I teach, though, despite
careful explanation of the conversions for scanf, there's typically someone
who'll use %f for doubles. I think it is a natural mistake, compounded by
the fact that trying to remember the conversions mnemonically by data type
is a non-starter [you mean %d isn't for doubles? ]
-leor

>
>Dan

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM steve Python 4 03-13-2005 12:34 AM JustSomeGuy C++ 5 06-06-2004 04:16 PM Snubis C++ 0 01-02-2004 01:53 PM

Advertisments