Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ctype(3)

setlocale(3)

scanf(3s)

environ(5int)

atof(3)

Name

atof, atoi, atol, strtol, strtoul, strtod − convert ASCII to numbers

Syntax

#include <stdlib.h>

double atof(nptr)
char *nptr;

atoi(nptr)
char *nptr;

long atol(nptr)
char *nptr;

long strtol(nptr, eptr, base)
char *nptr, **eptr;
int base;

unsigned long strtoul(nptr, eptr, base)
char *nptr, **eptr;
int base;

double strtod (nptr, eptr)
char *nptr, **eptr;

unsigned long strtoul(nptr, eptr, base)
char *nptr, **eptr;
int base;

Description

These functions convert a string pointed to by nptr to floating, integer, and long integer representation respectively.  The first unrecognized character ends the string. 

The atof function recognizes (in order), an optional string of spaces, an optional sign, a string of digits optionally containing a radix character, an optional ‘e’ or ‘E’, and then an optionally signed integer. The atof function is defined in both <stdlib.h> and <math.h>.

The atoi and atol functions recognize (in order), an optional string of spaces, an optional sign, then a string of digits. The atol function is defined in both <stdlib.h> and <math.h>.

The strtol function returns as a long integer, the value represented by the character string nstr.  The string is scanned up to the first character inconsistent with the base.  Leading white-space characters are ignored. 

If the value of eptr is not (char **) NULL, a pointer to the character terminating the scan is returned in **eptr.  If no integer can be formed, **eptr is set to nstr , and zero is returned. 

If base is positive and not greater than 36, it is used as the base for conversion.  After an optional leading sign, leading zeros are ignored, and 0x or 0X is ignored if base is 16. 

If base is zero, the string itself determines the base thus:  After an optional leading sign, a leading zero indicates octal conversion, and a leading 0x or 0X hexadecimal conversion.  Otherwise, decimal conversion is used. 

Truncation from long to int can take place upon assignment, or by an explicit cast. 

The strtoul function is the same as strtol except that strtoul returns, as an unsigned long integer, the value represented by the character string nstr. 

The strtod function returns as a double-precision floating point number, the value represented by the character string pointed to by nptr.  The string is scanned up to the first unrecognized character.  The strtod function is defined in both <stdlib.h> and <math.h>.

The strtod function recognizes an optional string of white-space characters, as defined by isspace in ctype, then an optional sign, then a string of digits optionally containing a radix character, then an optional e or E followed by an optional sign or space, followed by an integer. 

If the value of eptr is not (char **)NULL, a pointer to the character terminating the scan is returned in the location pointed to by eptr.  If no number can be formed, *eptr is set to nptr, and zero is returned. 

The radix character for atof and strtod is that defined by the last successful call to setlocale category LC_NUMERIC. If setlocale category LC_NUMERIC has not been called successfully, or if the radix character is not defined for a supported language, the radix character is defined as a period (.).

International Environment

LC_CTYPEIf this environment variable is set and valid, strtod uses the international language database named in the definition to determine character classification rules.

LC_NUMERICIf this environment is set and valid, atof and strtod use the international language database named in the definition to determine radix character rules.

LANGIf this environment variable is set and valid atof and strtod use the international language database named in the definition to determine collation and character classification rules.  If LC_CTYPE or LC_NUMERIC is defined, their definition supercedes the definition of LANG.

Diagnostics

The atof function returns HUGE if an overflow occurs, and  a 0 value if an underflow occurs, and sets errno to ERANGE.  HUGE is defined in <math.h>.

The atoi function returns INT_MAX or INT_MIN (according to the sign of the value) and sets errno to ERANGE, if the correct value is outside the range of values that can be represented. 

The atol function returns LONG_MAX or LONG_MIN (according to the sign of the value) and sets errno to ERANGE, if the correct value is outside the range of values that can be represented. 

The strtol function returns LONG_MAX or LONG_MIN (according to the sign of the value) and sets errno to ERANGE, if the correct value is outside the range of values that can be represented. 

The strtoul function returns ULONG_MAX and sets errno to ERANGE, if the correct value is outside the range of values that can be represented. 

The strtod function returns HUGE (according to the sign of the value), and sets errno to ERANGE if the correct value would cause overflow.  A 0 is returned and errno is set to ERANGE if the correct value would cause underflow. 

See Also

ctype(3), setlocale(3), scanf(3s), environ(5int)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026