Chapter 10. General Utilities <stdlib.h>

Table of Contents
10.1. String Conversion Functions
10.2. Pseudo-Random Sequence Generation Functions
10.3. Memory Management Functions
10.4. Communication with the Environment
10.5. Searching and Sorting Utilities
10.6. Integer Arithmetic Functions
10.7. Multi-byte Character Functions
10.8. Multi-byte String Functions

The header <stdlib.h> declares four types and several functions of general utility, and defines several macros.

The types declared are size_t and wchar_t (both described in the ANSI specification section 7.1.6),

div_t

which is a structure type that is the type of the value returned by the div function, and

ldiv_t

which is a structure type that is the type of the value returned by the ldiv function.

The macros defined are NULL (described in in the ANSI specification section 7.1.6),

EXIT_FAILURE

and

EXIT_SUCCESS, which expand to integral expressions that may be used as the argument to the exit function to return unsuccessful or successful termination status respectively, to the host environment,

RAND_MAX which expands to an integral constant expression, the value of which is the maximum value returned by the rand function, and

MB_CUR_MAX which expands to a positive integer expression whose value is the maximum number of bytes in a multi-byte character for the extended character set specified by the current locale (category LC_TYPE), and whose value is never greater than MB_LEN_MAX

10.1. String Conversion Functions

10.1.1. The atof function

Synopsis
#include <stdlib.h>

double atof (const char * iptr );

Description

The atof function converts the initial portion of the string pointed to by iptr to double. Except for the behavior on error, it is equivalent to

strtod (nptr, (char **)NULL)
Returns

The atof function returns the converted value as a double length floating point number.

See Also

Section 10.1.4

10.1.2. The atoi function

Synopsis
#include <stdlib.h>

int atoi (const char * nptr );

Description

The atoi function converts the initial portion of the string pointed to by nptr to int representation. Except for the behavior in error, it is equivalent to

(int)strtol (nptr, (char **)NULL, 10)
Returns

The atoi function returns the converted value.

See Also

Section 10.1.5

10.1.3. The atol function

Synopsis
#include <stdlib.h>

long atol (const char * nptr );

Description

The atol function converts the initial portion of the string pointed to by nptr to long int representation. Except for the behavior in error, it is equivalent to

strtol (nptr, (char **)NULL, 10)
Returns

The atol function returns the converted value.

See Also

Section 10.1.5

10.1.4. The strtod function

Synopsis
#include <stdlib.h>

double strtod (const char * nptr , char ** endptr );

Description

The strtod function converts the initial portion of the string pointed to by nptr to double representation. First it decomposes the string into three parts: an initial, possibly empty, sequence of white space characters (as specified by the isspace function), a subject sequence resembling a floating point constant: and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then it attempts to convert the subject sequence to a floating point number, and return the result.

The expected form of the subject sequence is an optional plus or minus sign, then a non-empty sequence of digits optionally containing a decimal-point character, then an optional exponent part as defined in the ANSI specification section 6.1.3.1, but no floating suffix. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists of entirely white space, or if the first non-white space character is other than a sign, a digit or a decimal point.

Returns

The strtod function returns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, plus of minus HUGE_VAL is returned (according to the sign of the value), and the value of the macros ERANGE is stored in errno. If the correct value would cause underflow, zero is returned and the value of the macro ERANGE is store in errno.

Implementation Notes
  • In GCC-1750, the accuracy of the result is generally better than 10 digits. In most cases, 13 decimal digits is sufficient to represent any of the 2**48 values of the M1750 extended precision floating point format. Powers of 10 within the range of the M1750 extended precision format (roughly 1.0e-38 to 1.0e+38) are converted exactly.

10.1.5. The strtol function

Synopsis
#include <stdlib.h>

long strtol (const char * nptr , char ** endptr , int base );

Description

The strtol function converts the initial portion of the string pointed to by nptr to long int representation. First it decomposes the input string into three parts: an initial, possibly empty, sequence of white space characters (as specified by the The isspace function Section 3.1.9), a subject sequence resembling an integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then it attempts to convert the subject sequence to an integer, and returns the result.

Returns

The strtol function returns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, LONG_MAX or LONG_MIN is returned (according to the sign of the value), and the value of the macro ERANGE is is stored in errno.

10.1.6. The strtoul function

Synopsis
#include <stdlib.h>

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

Description

The strtoul function converts the initial portion of the string pointed to by nptr to unsigned long int representation. First it decomposes the input string into three parts: an initial, possibly empty, sequence of white space characters (as specified by the Section 3.1.9, a subject sequence resembling an unsigned integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then it attempts to convert the subject sequence to an unsigned integer, and returns the result.

Returns

The strtoul function returns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, ULONG_MAX is returned, and the value of the macro ERANGE is is stored in errno.