How to convert a string to a number in C language

Source: Internet
Author: User

The C language provides several standard library functions that can convert a string to a number of any type (integer, long, float, and so on). The following is an example of converting a string to an integer using the Atoi () function:

# include <stdio. H>
# include <stdlib. H>
void main (void);
void Main (void)
{
int num;
char * str = "100";
num = atoi (str);
printf ("The string ' str ' is%s and the number ' num ' is%d. \ n",
STR, num);
}

The Atoi () function has only one argument, which is the string to be converted to a number. The return value of the atoi () function is the converted integer value.

The following functions can convert a string to a number:
------------------------------------------------------------------------
Function name action
------------------------------------------------------------------------
Atof () Converts a string to a double-precision floating-point value
Atoi () Converts a string to an integer value
ATOL () Converts a string to a long integer value
Strtod () Converts a string to a double-precision floating-point value and reports all remaining digits that cannot be converted
Strtol () Converts a string to a long integer value and reports all remaining digits that cannot be converted
Strtoul () Converts a string to an unsigned long value and reports all remaining digits that cannot be converted
------------------------------------------------------------------------

When you convert a string to a number, it can cause overflow, and if you use a function such as Strtoul (), you can check for this overflow error. Take a look at the following example:
# include <stdio. H>
# include <stdlib. H>
# include <limits. H>
void main (void);
void Main (void)
{
char* str = "1234567891011121314151617181920";
unsigned long num;
char * leftover;
num = Strtoul (str, &leftover, 10);
printf ("Original string:%s\n", str);
printf ("Converted number:%1u\n", num);
printf ("Leftover characters:%s\n", leftover);
}

In the example above, the string to be converted is too long to exceed the value range of the unsigned long value, so the Strtoul () function returns Ulong_max (4294967295) and causes the. Char leftover points to the part of the character that caused the overflow in the string, and the Strtoul () function also assigns the global variable errno to Erange to notify the caller of the function that an overflow error occurred. The functions strtod () and strtol () handle overflow errors in exactly the same way as functions Strtoul (), and you can learn more about the details of these three functions from the compiled program documentation.

How to convert a string to a number in C language

Related Article

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.