473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 46709
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Julián Albo | last post by:
Hello. This test: #include <stdio.h> int main () { double a= -1.0e-120; if (a < 0.0)
0
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET...
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
3
by: Tomasz Bednarz | last post by:
Can someone help me to parse double precision numbers from text file? I have a sample text file which is as follows: VARIABLES = x, y ZONE I=11, J=11 1.10000000000 0.10000000000...
1
by: Martin Pöpping | last post by:
Hello, I´ve a problem with parsing a double value from an xml file. My code looks like this: int concept_id; double rank; XmlElement root = documentXMLString.DocumentElement; XmlNodeList...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
3
by: devecibasi | last post by:
Hi, I am trying to use ShellExecute function to invoke the ax program. The code below works perfect: ShellExecute(0&, "open", "ax.exe", "C:\2runtemp2.run", "C:\axfolder", 5) The code below...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.