atof, atoi, atol, _atold

Description

Convert strings to double (atof), long double (_atold), integer (atoi), or long (atol).

#include <math.h> atof, _atold  
#include <stdlib.h> atof, _atold, atoi, atol  

double atof( const char *string );

long double _atold( const char *string );

int atoi( const char *string);

long atol( const char *string);

string String to be converted  

Remarks

These functions convert a character string to a double-precision floating-point value (atof), an integer value (atoi), a long integer value (atol), or a long double value (_atold). The input string is a sequence of characters that can be interpreted as a numerical value of the specified type.

The string size that can be handled by the atof or _atold function is limited to 100 characters.

The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('\0') terminating the string.

The atof and _atold functions expect string to have the following form:

[[whitespace]] [[sign]] [[digits]] [[.digits]] [[{d |D | e | E}[[sign]]digits]]

A whitespace consists of space and/or tab characters, which are ignored; sign is either plus (+) or minus (–); and digits are one or more decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter (d, D, e, or E) and an optionally signed decimal integer.

The atoi and atol functions do not recognize decimal points or exponents. The string argument for these functions has the form

[[whitespace]] [[sign]]digits

where whitespace, sign, and digits are exactly as described above for atof.

Return Value

Each function returns the double, long double, int, or long value produced by interpreting the input characters as a number. The return value is 0 (for atoi), 0L (for atol), and 0.0 (for atof and _atold) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow.

Compatibility

atof, atoi, atol

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_atold

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

_ecvt, _fcvt, _gcvt, strtod

Example

/* ATOF.C: This program shows how numbers stored as strings can be

* converted to numeric values using the atof, atoi, and atol functions.

*/

#include <stdlib.h>

#include <stdio.h>

void main( void )

{

char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */

x = atof( s );

printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210"; /* Test of atof */

x = atof( s );

printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs"; /* Test of atoi */

i = atoi( s );

printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars"; /* Test of atol */

l = atol( s );

printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );

}

Output

atof test: ASCII string: -2309.12E-15 float: -2.309120e-012

atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210

atoi test: ASCII string: -9885 pigs integer: -9885

atol test: ASCII string: 98854 dollars long: 98854