Converting char * to float using sscanf()

This is a discussion on Converting char * to float using sscanf() within the C Programming forums, part of the General Programming Boards category; Is it possible to convert char * to float using sscanf()? I tried this but it doesn't work. Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Converting char * to float using sscanf()

    Is it possible to convert char * to float using sscanf()? I tried this but it doesn't work.
    Code:
    #include <stdio.h>
    
    int main()
    {
        float number;
        char price[] = "49.99";
    
        sscanf(price, "%lf", number);
    
        printf("%lf", number);
    
        return 0;
    }
    Thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    sscanf(price, "%f", &number);
    printf("%f", number);

    and if you're interested:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Or even add the compiler option
    gcc -Wall
    if you're using gcc/dev-c++/mingw/cygwin
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  3. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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