Thread: type casting

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

    Question type casting

    Hi all,
    Sorry for the stupid question... but I havn't used C very often...
    How do I convert a string into float?
    THANKS!!!
    Liat.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by liats80 View Post
    Hi all,
    Sorry for the stupid question... but I havn't used C very often...
    How do I convert a string into float?
    THANKS!!!
    Liat.
    Many possibilities. sscanf(), strtod(), strtof(), atof() ...

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    Thanks!!!
    I tried atof() and that seems to work fine.
    Any major resons to use something else? (are there advantages for using the others?)
    Thanks again!
    Liat.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    strtod() and strtof() allow you to detect if the string isn't a double or float respectively and be able to write a complicated routine to figure out how you want to proceed.

    sscanf() has more or less the same limitations of scanf(). It allows some error handling but not as much as strtod() or strtof().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 06:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM