473,406 Members | 2,867 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,406 software developers and data experts.

Simple problem?

What's wrong with this code? Using Gcc in Fedora 4 it gives the wrong value
for x unless it is declared a float.

#include <stdio.h>
int main{
char * estr = "1.234";
double x = 0;
sscanf(estr, "%g", &x);
printf("%g", x);
return 0;
}

typical result is x = -0.0140015.

Thanks,
Shel Sherman

Aug 21 '06 #1
4 1216
"Shel Sherman" <ss******@sbcglobal.netwrites:
double x = 0;
sscanf(estr, "%g", &x);
Use the "l" flag with the scanf functions to read doubles.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Aug 21 '06 #2
Shel Sherman wrote:
What's wrong with this code? Using Gcc in Fedora 4 it gives the wrong value
for x unless it is declared a float.

#include <stdio.h>
int main{
char * estr = "1.234";
double x = 0;
sscanf(estr, "%g", &x);
printf("%g", x);
return 0;
}

typical result is x = -0.0140015.
After fixing the three obvious errors in your code, yielding:

#include <stdio.h>
int main(void)
{ /* mha: fixed missing parameter list */
char *estr = "1.234";
double x = 0;
sscanf(estr, "%lg", &x); /* mha: fixed format specifier */
printf("%g\n", x); /* mha: added EOL character */
return 0;
}

I get as output:

1.234
Aug 21 '06 #3
Thanks all. That did the trick. Appreciate the help.

Shel

"Martin Ambuhl" <ma*****@earthlink.netwrote in message
news:4k************@individual.net...
Shel Sherman wrote:
>What's wrong with this code? Using Gcc in Fedora 4 it gives the wrong
value for x unless it is declared a float.

#include <stdio.h>
int main{
char * estr = "1.234";
double x = 0;
sscanf(estr, "%g", &x);
printf("%g", x);
return 0;
}

typical result is x = -0.0140015.

After fixing the three obvious errors in your code, yielding:

#include <stdio.h>
int main(void)
{ /* mha: fixed missing parameter list */
char *estr = "1.234";
double x = 0;
sscanf(estr, "%lg", &x); /* mha: fixed format specifier */
printf("%g\n", x); /* mha: added EOL character */
return 0;
}

I get as output:

1.234

Aug 21 '06 #4
Shel Sherman wrote:
>
Thanks all. That did the trick. Appreciate the help.
Don't top-post. Read the links below for reasons, etc.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Aug 21 '06 #5

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
6
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I donīt know much javascript so I wrote a very simple one to validate a form I have on my webpage. ...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
18
by: Sender | last post by:
Yesterday there was a very long thread on this query. (You can search on this by post by 'sender' with subject 'Simple Problem' post date Oct 7 time 1:43p) And in the end the following code was...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
2
by: Vitali Gontsharuk | last post by:
Hi! I have a problem programming a simple client-server game, which is called pingpong ;-) The final program will first be started as a server (nr. 2) and then as a client. The client then...
8
by: rdrink | last post by:
I am just getting into pysqlite (with a fair amount of Python and MySQL experience behind me) and have coded a simple test case to try to get the hang of things... yet have run into a 'stock...
5
by: Chelong | last post by:
hey,the follow is the text file content ========================================apple====pear== one Lily 7 0 0 7 7 two Lily 20 20 6.6666 20 8 one Lily 0 10 2.85 4 0 two Lily 22 22 7.33326 2 5 ...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.