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. 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.
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)
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
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.
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)
"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
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
Thank you all. And indeed I should first read the C-faq to avoid such
a naive mistake. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Julián Albo |
last post by:
Hello.
This test:
#include <stdio.h>
int main ()
{
double a= -1.0e-120;
if (a < 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 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:
|
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
|
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
|
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;
| |
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;
|
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)
|
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,...
|
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...
|
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...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |