[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem converting a char array to a float

Posted on 2006-05-04
7
Medium Priority
?
242 Views
Last Modified: 2012-05-05
Seems like this should be pretty simple, but I can't get it to work.  Here's my code:

rply.value is a char array in a rply struct.  The value of the float is 50.5, but I'm getting a value of 0.000000 after I do an atof();

      float t;
      t=atof(rply.value);//<<<< rply.value is a char[64].  

      printf("Value of float %f\n",t);///<<<< prints out the number above
      printf("Value in string %s\n",rply.value); ///<<< prints out 50.5

Thanks
0
Comment
Question by:ChrisClement
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16605522
atof returns a double .... Use a double instead

     double t;
     t=atof(rply.value);//<<<< rply.value is a char[64].  

     printf("Value of float %f\n",t);///<<<< prints out the number above
     printf("Value in string %s\n",rply.value); ///<<< prints out 50.5
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16605563
Alternatively you can use strtod (recommended since it can detect errors and hence doubles up as your validation function) to convert to double

or

use atoff(non-standard) to convert to float
0
 

Author Comment

by:ChrisClement
ID: 16605624
Thanks, but the result is the same.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 total points
ID: 16605666
try this piece of code

#include <stdio.h>
#include <stdlib.h>

int main ()
{
      char value[] = "50.5";
      double d = atof(value);

      printf("%s %f",value,d);
}

Does this work fine ?
0
 

Author Comment

by:ChrisClement
ID: 16605805
Yeah, it worked.  My problem was that I wasn't including the the same libraries that you did.  

Thanks for the help, sunny.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16605837
Glad to help :o)

> I wasn't including the the same libraries that you did.
In that case compiler should have thrown a warning ...
0
 

Author Comment

by:ChrisClement
ID: 16606336
Yeah, I would think so.  Don't know why it didn't.  I'm using cc on PC-BSD.  Go figure.  
0

Featured Post

Independent Software Vendors: 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

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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

872 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