How to convert a string to a number in C?

Source: Internet
Author: User
Atof (converts a string to a float number)
Related functions: atoi, atol, strtodd, strtol, strtoul

Header file # include <stdlib. h>

Define the function Double atof (const char * nptr );

Function Description: atof () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and the result is returned. The nptr string can contain positive and negative numbers, decimal points, or E (e) to represent the exponential part, such as 123.456 or 123e-2.

Return the number of floating point types after conversion.

The additional description atof () is the same as the use of strtodd (nptr, (char **) null.

Example/* convert string a and string B into numbers and add them together */

# Include <stdlib. h> main () {char * A = "-100.23"; char * B = "200e-2"; float C; C = atof (A) + atof (B ); printf ("C = %. 2f \ n ", c) ;}execute c =-98.23

 
Refer to Baidu knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.