Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Problem: scanf used for double (http://www.velocityreviews.com/forums/t315804-problem-scanf-used-for-double.html)

Frank Chow 10-16-2003 03:58 PM

Problem: scanf used for double
 
Please see the following code:

/* main.c */

#include <stdio.h>

int main()
{
double d;
scanf("%f", &d);
printf("%g\n", d);
}

The problem is, whatever I input to stdin (even an illegal data), the
program just print a number that seems to be a data from "raw memory",
ie uninitialized memory (for example: "5.35162e-315").

And if I replace the statement "double d" with "double d = 3.14", then
I'll always get the output "3.14" for whatever I input (even an
illegal data).

Then I may get the conclusion that the "scanf" statement never does
its job. And I have tried this under 3 compilers and none of them give
the right result.

Please give me some explanation about this.

Thanks.

Eric Sosman 10-16-2003 04:08 PM

Re: Problem: scanf used for double
 
Frank Chow wrote:
>
> Please see the following code:
>
> /* main.c */
>
> #include <stdio.h>
>
> int main()
> {
> double d;
> scanf("%f", &d);


scanf("%lf", &d)

See Question 12.13 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html


--
Eric.Sosman@sun.com

Christopher Benson-Manica 10-16-2003 04:08 PM

Re: Problem: scanf used for double
 
Frank Chow <fang_chow@yahoo.com> spoke thus:

> #include <stdio.h>


> int main()
> {
> double d;
> scanf("%f", &d);
> printf("%g\n", d);
> }


If you had your compiler warnings turned on, you probably would have gotten
something like

test.c:6: warning: float format, double arg (arg 2)

The correct conversion specifier for doubles is %lf when using scanf. Also,
should return a value from main, a la

return( EXIT_SUCCESS );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Irrwahn Grausewitz 10-16-2003 04:55 PM

Re: Problem: scanf used for double
 
Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:

<snip>
> Also,
>should return a value from main, a la
>
>return( EXIT_SUCCESS );


after inclusion of stdlib.h, of course; alternatively

return 0;

is fine, too.
--
Irrwahn
(irrwahn33@freenet.de)

Dan Pop 10-16-2003 05:07 PM

Re: Problem: scanf used for double
 
In <bec47b4b.0310160758.753e1afd@posting.google.com > fang_chow@yahoo.com (Frank Chow) writes:

>Please see the following code:
>
>/* main.c */
>
>#include <stdio.h>
>
>int main()
>{
> double d;
> scanf("%f", &d);
> printf("%g\n", d);
>}
>
>The problem is, whatever I input to stdin (even an illegal data), the
>program just print a number that seems to be a data from "raw memory",
>ie uninitialized memory (for example: "5.35162e-315").


Where did you get the idea that %f is the right conversion specifier for
a double? If you needed a float instead, what would you use?

For obvious reasons, scanf is not a perfect mirror of printf.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de

Christopher Benson-Manica 10-16-2003 05:30 PM

Re: Problem: scanf used for double
 
Irrwahn Grausewitz <irrwahn33@freenet.de> spoke thus:

> return 0;
> is fine, too.


I thought it was non-portable :(

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Irrwahn Grausewitz 10-16-2003 07:13 PM

Re: Problem: scanf used for double
 
Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:

>Irrwahn Grausewitz <irrwahn33@freenet.de> spoke thus:
>
>> return 0;
>> is fine, too.

>
>I thought it was non-portable :(


Nope, according to ISO/IEC 9899:1999 5.1.2.2.3 and 7.20.4.3#5
it is perfectly portable.

Regards
--
Irrwahn
(irrwahn33@freenet.de)

Glen Herrmannsfeldt 10-16-2003 07:18 PM

Re: Problem: scanf used for double
 

"Frank Chow" <fang_chow@yahoo.com> wrote in message
news:bec47b4b.0310160758.753e1afd@posting.google.c om...
> Please see the following code:
>
> /* main.c */
>
> #include <stdio.h>
>
> int main()
> {
> double d;
> scanf("%f", &d);
> printf("%g\n", d);
> }
>
> The problem is, whatever I input to stdin (even an illegal data), the
> program just print a number that seems to be a data from "raw memory",
> ie uninitialized memory (for example: "5.35162e-315").


Others have said that %f is wrong for double, which is true.

Note that if scanf() doesn't find valid data to convert to a number it
doesn't store anything. Also, it returns the number of valid conversions,
which you can check.

-- glen





Martin Ambuhl 10-16-2003 11:34 PM

Re: Problem: scanf used for double
 
Frank Chow wrote:
> Please see the following code:
>
> /* main.c */
>
> #include <stdio.h>
>
> int main()
> {
> double d;
> scanf("%f", &d);


You mean scanf("%lf", &d);




--
Martin Ambuhl


Frank Chow 10-17-2003 05:21 AM

Re: Problem: scanf used for double
 
Thank you all. And indeed I should first read the C-faq to avoid such
a naive mistake.


All times are GMT. The time now is 04:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.