Solved

atof() and sscanf() do not work for me

Posted on 2002-07-09
3
416 Views
Last Modified: 2009-12-16
I'm trying to convert a string into a double.  I've tried both the atof() and sscanf() functions with no success.  What am I doing wrong?

I am running on a Solaris 2.8 UNIX box.

Here is the code:

#include <stdio.h>
#include <string.h>
main() {
  char numstring[] = "12.34";
  double d;
  d = atof(numstring);
  printf("String = %s\n",numstring);
  printf("Double = %f\n",d);
}

Here are the results:

String = 12.34
Double = -4264242.000000

Playing around with the format string in the second printf statement seems to change the answer, but not to 12.34.

Using sscanf() instead of atof() yields different incorrect results.

Any ideas?
0
Comment
Question by:dastrw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 7142542
#include <stdlib.h>
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 7143200
You should be using the long float formater for the seconds printf:

#include <stdio.h>
#include <string.h>
main() {
 char numstring[] = "12.34";
 double d;
 d = atof(numstring);
 printf("String = %s\n",numstring);
 printf("Double = %lf\n",d);  // %f should be %lf
}

Good Luck,
Steve
0
 
LVL 2

Author Comment

by:dastrw
ID: 7143930
Thanks.  I must have zoned and thought stdio.h was the same as stdlib.h!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with MFCApp 78 469
rhel6 C system() call to zip to archive has problems 25 255
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 69
how to understand recursion 12 238
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question