C language Some functions that convert a string to a number summary _c language

Source: Internet
Author: User
Tags stdin

C language Atoi () function: Converts a string to int (integer)
header file:

#include <stdlib.h>

The Atoi () function is used to convert a string to an integer (int), and its prototype is:

int atoi (const char * str);

The function description Atoi () function scans the parameter str string, skip the preceding whitespace characters (such as spaces, tab indents, etc., can be detected by the Isspace () function) until a number or positive sign is encountered before the conversion begins, and then when a non-numeric or string is terminated. To end the conversion and return the result.

return value returns the converted integer number, and returns 0 if STR cannot be converted to int or STR is an empty string.

Example: Adds a string A and a string B to a number after it is converted.

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 int i;
 Char buffer[256];
 printf ("Enter a number:");
 Fgets (buffer, 256, stdin);
 i = atoi (buffer);
 printf ("The value entered is%d.", i);
 System ("pause");
 return 0;
}

Execution results:

Enter a Number:233cyuyan The
value entered is 233.


C language Atof () function: Converts a string to double (double-precision floating-point number)
header file:

#include <stdlib.h>

The name of the ATOL () function is derived from "ASCII to Long", which converts the string to the growth integer (long), which is a prototype:

  Long ATOL (const char * str);

The function Description Atol () scans the parameter str string, skipping the preceding whitespace characters (such as spaces, tab indents, etc.) can be detected by the isspace () function until a number or positive sign is turned on, and then the conversion is not finished until the number or end of the string is encountered. and returns the result.

Return value returns the number of long integers after the conversion (long) and returns 0 if STR cannot be converted to long or the STR is an empty string.

Example: Converts the input string to long.

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 long int li;
 Char buffer[256];
 printf ("Enter A long Number:");
 Fgets (buffer, 256, stdin);
 Li = atol (buffer);
 printf ("The value entered is%ld.\n", Li);
 System ("pause");
 return 0;
}

Execution results:

Enter a long Number:1200cyuyan the
value entered is 1200.


C language Atof () function: Converts a string to double (double-precision floating-point number)
header file:

#include <stdlib.h>

The function atof () is used to convert a string to a double-precision floating-point number (double), which is a prototype:

Double atof (const char* str);

The name of Atof () is derived from the abbreviation for ASCII to floating point numbers, which scans the parameter str string, skipping the preceding whitespace characters (such as spaces, tab indents, and so on, which can be detected by the Isspace () function), The conversion is not done until a number or sign is encountered, and then the conversion is not completed until the end of the number or string (' "), and the result is returned. The parameter str string can contain a positive sign, a decimal point, or E (e) to represent the exponent portion, such as 123. 456 or 123e-2.

return value returns the converted floating-point number, and returns 0.0 if string str cannot be converted to double.

Warm tip: ANSI C specification defines the Stof (), Atoi (), Atol (), strtod (), Strtol (), Strtoul () a total of 6 functions that can convert strings to numbers, you can compare learning; use Atof () and use Strtod ( STR, NULL) The result is the same. In addition, there are 5 additional functions in the C99/C++11 specification, namely, Atoll (), Strtof (), Strtold (), Strtoll (), Strtoull (), which do not introduce, please learn by yourselves.

Example:

#include <stdio.h>
#include <stdlib.h>
int main () {
 char *a = " -100.23",
   *b = "200e-2",
   *c = "341",
   *d = "100.34cyuyan",
   *e = "Cyuyan";
 printf ("A =%.2f\n", Atof (a));
 printf ("B =%.2f\n", atof (b));
 printf ("c =%.2f\n", Atof (c));
 printf ("D =%.2f\n", atof (d));
 printf ("E =%.2f\n", Atof (e));
 System ("pause");
 return 0;
}

Execution results:

A = -100.23
b = 2.00
c = 341.00
d = 100.34
E = 0.00

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.