473,786 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 46742
Frank Chow <fa*******@yaho o.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)cybers pace.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.c yberspace.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*******@free net.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*******@free net.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #6
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Irrwahn Grausewitz <ir*******@free net.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*******@free net.de)
Nov 13 '05 #7

"Frank Chow" <fa*******@yaho o.com> wrote in message
news:be******** *************** ***@posting.goo gle.com...
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6204
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
1456
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 projects; its generated assembly is inside MyAssembly.dll When used inside a VC++.NET project it appears correctly over forms at design time but when trying to compile the project we get the following error:
11
3521
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 casting (explicitly / implicitly). Code looks as below : const double d_a = 100e-9; // 100ns const double d_b = 20e-9; // 20ns
3
1668
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 0.10000000000 0.20000000000 0.20000000000 0.30000000000 0.30000000000 0.40000000000
1
3710
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 records = root.ChildNodes;
3
2131
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 the DLL to get some results back from it. My Structure In C# looks like this: public struct InputPurchaseOrder { public System.String poJobName;
3
3866
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 does NOT work: ShellExecute(0&, "open", "ax.exe", "C:\Documents and Settings\jon\Desktop\requiredfiles\2runtemp2.run", "C:\axfolder", 5)
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.