472,135 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

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.
Nov 13 '05 #1
16 46599
Frank Chow <fa*******@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.
Nov 13 '05 #2
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
--
Er*********@sun.com
Nov 13 '05 #3
Christopher Benson-Manica <at***@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
(ir*******@freenet.de)
Nov 13 '05 #4
In <be**************************@posting.google.com > fa*******@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: Da*****@ifh.de
Nov 13 '05 #5
Irrwahn Grausewitz <ir*******@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.
Nov 13 '05 #6
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Irrwahn Grausewitz <ir*******@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
(ir*******@freenet.de)
Nov 13 '05 #7

"Frank Chow" <fa*******@yahoo.com> wrote in message
news:be**************************@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


Nov 13 '05 #8
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

Nov 13 '05 #9
Thank you all. And indeed I should first read the C-faq to avoid such
a naive mistake.
Nov 13 '05 #10
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Nope, according to ISO/IEC 9899:1999 5.1.2.2.3 and 7.20.4.3#5
it is perfectly portable.


<dumb>So if 0 is portable, why the EXIT_SUCCESS macro?</dumb>

--
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.
Nov 13 '05 #11
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Nope, according to ISO/IEC 9899:1999 5.1.2.2.3 and 7.20.4.3#5
it is perfectly portable.


<dumb>So if 0 is portable, why the EXIT_SUCCESS macro?</dumb>


Because it's more descriptive and has a portable(!) and descriptive
counter-part, EXIT_FAILURE.

ISO/IEC 9899:1999 7.20.4.3
5 [...] If the value of status is zero or EXIT_SUCCESS, an
implementation-defined form of the status successful termination is
returned. If the value of status is EXIT_FAILURE, an implementation-
defined form of the status unsuccessful termination is returned.
Otherwise the status returned is implementation-defined.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #12
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Because it's more descriptive and has a portable(!) and descriptive
counter-part, EXIT_FAILURE.


So it's kind of like the unary + thing, symmetry and all? I dunno, I always
thought the l337-ness of C code was directly related to how obfuscated it
looked ;)

--
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.
Nov 13 '05 #13
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Because it's more descriptive and has a portable(!) and descriptive
counter-part, EXIT_FAILURE.
So it's kind of like the unary + thing, symmetry and all?


Hm, well, yes and no. I can remember a discussion about this in c.l.c
or c.s.c, where the claim was made that in the context of program exit
codes 0 may describe a kind of successful termination different from
what EXIT_SUCCESS results in. Whatever that means, practically.
I dunno, I always
thought the l337-ness of C code was directly related to how obfuscated it
looked ;)


:)

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #14
In <bm**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Because it's more descriptive and has a portable(!) and descriptive
counter-part, EXIT_FAILURE.


So it's kind of like the unary + thing, symmetry and all?


Exactly.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #15
In <gb********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
Because it's more descriptive and has a portable(!) and descriptive
counter-part, EXIT_FAILURE.


So it's kind of like the unary + thing, symmetry and all?


Hm, well, yes and no. I can remember a discussion about this in c.l.c
or c.s.c, where the claim was made that in the context of program exit
codes 0 may describe a kind of successful termination different from
what EXIT_SUCCESS results in. Whatever that means, practically.


Nothing at all, in the context of portable programming. Furthermore, the
most natural interpretation of the standard is that both are mapped to
the *same* form of successful termination.

5 Finally, control is returned to the host environment. If the
value of status is zero or EXIT_SUCCESS, an implementation-defined
^^
form of the status successful termination is returned.

The standard talks about a *single* "implementation-defined form of
the status successful termination".

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #16
Da*****@cern.ch (Dan Pop) wrote:
In <gb********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:


<snip>
Hm, well, yes and no. I can remember a discussion about this in c.l.c
or c.s.c, where the claim was made that in the context of program exit
codes 0 may describe a kind of successful termination different from
what EXIT_SUCCESS results in. Whatever that means, practically.


Nothing at all, in the context of portable programming. Furthermore, the
most natural interpretation of the standard is that both are mapped to
the *same* form of successful termination.

5 Finally, control is returned to the host environment. If the
value of status is zero or EXIT_SUCCESS, an implementation-defined
^^
form of the status successful termination is returned.

The standard talks about a *single* "implementation-defined form of
the status successful termination".


It's the most natural interpretation, I agree. I just remembered the
discussion, not the outcome...

--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Julián Albo | last post: by
3 posts views Thread by Tomasz Bednarz | last post: by
1 post views Thread by Martin Pöpping | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.