convert string to double

This is a discussion on convert string to double within the C Programming forums, part of the General Programming Boards category; Code: int main() { double x; char s[200]="22.453"; x=atof(s); printf("%lf",x); } why doesnt this give me the float value that ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    convert string to double

    Code:
    int main()
    {
    double x;
    char s[200]="22.453";
    x=atof(s);
    printf("%lf",x);
    
    
    }
    why doesnt this give me the float value that I need. How can I fix it
    the atof function called I thought would take "22.453"
    and get 22.453 and then let me put that into an integer.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    You need to #include <stdlib.h>. And #include <stdio.h> while you're at it. (Why you need to be told this, I don't know...)

    And also, atof is deprecated in favor of strtod. Replace with strtod(nptr, (char **)NULL);
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    > And also, atof is deprecated in favor of strtod. Replace with strtod(nptr, (char **)NULL);

    Actually, atof isn't deprecated with anything. It is still supported by the C standards.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Also, &#37;lf is not the correct format for printing a float/double.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by citizen View Post
    > And also, atof is deprecated in favor of strtod. Replace with strtod(nptr, (char **)NULL);

    Actually, atof isn't deprecated with anything. It is still supported by the C standards.
    For what it's worth, it is in BSD and OS X.

    And at any rate, strtod is the better function.

    (Also, gets is still supported by the C standards, so that's really a suboptimal argument to make.)
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Code:
    #include <stdio.h>
    
    int main()
    {
    double x;
    char s[200]="22.453";
    x=strtod(s, (char **)NULL);
    
    printf("&#37;lf",x);
    
    
    }
    returns

    -802490616.000000

    can you show me a working solution to this...coz I tried strtod and atof..
    ..

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by zx-1 View Post
    For what it's worth, it is in BSD and OS X.
    Really? I've lost a little respect for both, then. They have no business "deprecating" completely standard language features.

    And at any rate, strtod is the better function.
    Probably.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > Also, &#37;lf is not the correct format for printing a float/double.

    Then later, you repeat your original code
    > printf("%lf",x);

    Last chance saloon - drink up or leave town.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    so what should I use &#37;f?
    pretty interesting site Dave_Sinkula

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, &#37;f is used with printf() for floats and doubles, while scanf() uses %f for floats and %lf for doubles.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String to Double function
    By jaro in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 12:10 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 03:56 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21