7.4.12 <stdlib.h>

Defines standard functions for standard processing of C programs.
The following macros are implementation-defined.

Type

Definition Name

Description

Type (macro)

onexit_t

Indicates the type returned by the function registered by the onexit function and the type of the value returned by the onexit function.

div_t

Indicates the type of structure of the value returned by the div function.

ldiv_t

Indicates the type of structure of the value returned by the ldiv function.

lldiv_t

Indicates the type of structure of the value returned by the lldiv function.

Constant (macro)

RAND_MAX

Indicates the maximum value of pseudo-random integers generated by the rand function.

EXIT_SUCCESS

Indicates the successfully completed state.

Function

atof

Converts a number-representing string to a double type floating-point number.

atoi

Converts a decimal-representing string to an int type integer.

atol

Converts a decimal-representing string to a long type integer.

atoll

Converts a decimal-representing string to a long long type integer.

strtod

Converts a number-representing string to a double type floating-point number.

strtof

Converts a number-representing string to a float type floating-point number.

strtold

Converts a number-representing string to a long double type floating-point number.

strtol

Converts a number-representing string to a long type integer.

strtoul

Converts a number-representing string to an unsigned long type integer.

strtoll

Converts a number-representing string to a long long type integer.

strtoull

Converts a number-representing string to an unsigned long long type integer.

rand

Generates pseudo-random integers from 0 to RAND_MAX.

srand

Sets an initial value of the pseudo-random number sequence generated by the rand function.

calloc

Allocates a storage area and clears all bits in the allocated storage area to 0.

free

Releases specified storage area.

malloc

Allocates a storage area.

realloc

Changes the size of storage area to a specified value.

bsearch

Performs binary search.

qsort

Performs sorting.

abs

Calculates the absolute value of an int type integer.

Function

div

Carries out division of int type integers and obtains the quotient and remainder.

labs

Calculates the absolute value of a long type integer.

ldiv

Carries out division of long type integers and obtains the quotient and remainder.

llabs

Calculates the absolute value of a long long type integer.

lldiv

Carries out division of long long type integers and obtains the quotient and remainder.

 

Implementation-Defined Specifications

Item

Compiler Specifications

calloc, malloc, or realloc function operation when the size is 0.

NULL is returned.

 

atof

Converts a number-representing string to a double type floating-point number.

[Format]

#include <stdlib.h>
double atof (const char *nptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

[Return values]

Converted data as a double type floating-point number

[Remarks]

If the converted result overflows or underflows, errno is set.

Data is converted up to the first character that does not fit the floating-point data type.

The atof function does not guarantee the return value if an error such as an overflow occurs. When you want to acquire the guaranteed return value, use the strtod function.

 

atoi

Converts a decimal-representing string to an int type integer.

[Format]

#include <stdlib.h>
long atoi (const char *nptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

[Return values]

Converted data as an int type integer

[Remarks]

If the converted result overflows, errno is set.

Data is converted up to the first character that does not fit the decimal data type.

The atoi function does not guarantee the return value if an error such as an overflow occurs. When you want to acquire the guaranteed return value, use the strtol function.

 

atol

Converts a decimal-representing string to a long type integer.

[Format]

#include <stdlib.h>
long atol (const char *nptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

[Return values]

Converted data as a long type integer

[Remarks]

If the converted result overflows, errno is set.

Data is converted up to the first character that does not fit the decimal data type.

The atol function does not guarantee the return value if an error such as an overflow occurs. When you want to acquire the guaranteed return value, use the strtol function.

 

atoll

Converts a decimal-representing string to a long long type integer.

[Format]

#include <stdlib.h>
long long atoll (const char *nptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

[Return values]

Converted data as a long long type integer

[Remarks]

If the converted result overflows, errno is set.

Data is converted up to the first character that does not fit the decimal data type.

The atoll function does not guarantee the return value if an error such as an overflow occurs. When you want to acquire the guaranteed return value, use the strtoll function.

 

strtod

Converts a number-representing string to a double type floating-point number.

[Format]

#include <stdlib.h>
double strtod (const char *nptr, char **endptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent a floating-point number

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent a floating-point number: 0
If the string pointed by nptr begins with a character that represents a floating-point number: Converted data as a double type floating-point number

Abnormal: If the converted data overflows: HUGE_VAL with the same sign as that of the string before conversion
If the converted data underflows: 0

[Remarks]

The strtod function converts data, from the first digit or the decimal point up to the character immediately before the character that does not represent a floating-point number, into a double type floating-point number. However, if neither an exponent nor a decimal point is found in the data to be converted, the compiler assumes that the decimal point comes next to the last digit in the string. In the area pointed by endptr, the function sets up a pointer to the first character that does not represent a floating-point number. If some characters that do not represent a floating-point number come before digits, the value of nptr is set. If endptr is NULL, nothing is set in this area.

 

strtof

Converts a number-representing string to a float type floating-point number.

[Format]

#include <stdlib.h>
float strtof (const char *nptr, char **endptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent a floating-point number

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent a floating-point number: 0
If the string pointed by nptr begins with a character that represents a floating-point number: Converted data as a float type floating-point number

Abnormal: If the converted data overflows: HUGE_VALF with the same sign as that of the string before conversion
If the converted data underflows: 0

[Remarks]

If the converted result overflows or underflows, errno is set.

The strtof function converts data, from the first digit or the decimal point up to the character immediately before the character that does not represent a floating-point number, into a float type floating-point number. However, if neither an exponent nor a decimal point is found in the data to be converted, the compiler assumes that the decimal point comes next to the last digit in the string. In the area pointed by endptr, the function sets up a pointer to the first character that does not represent a floating-point number. If some characters that do not represent a floating-point number come before digits, the value of nptr is set. If endptr is NULL, nothing is set in this area.

 

strtold

Converts a number-representing string to a long double type floating-point number.

[Format]

#include <stdlib.h>
long double strtold (const char *nptr, char **endptr);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent a floating-point number

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent a floating-point number: 0
If the string pointed by nptr begins with a character that represents a floating-point number: Converted data as a long double type floating-point number

Abnormal: If the converted data overflows: HUGE_VALL with the same sign as that of the string before conversion
If the converted data underflows: 0

[Remarks]

If the converted result overflows or underflows, errno is set.

The strtold function converts data, from the first digit or the decimal point up to the character immediately before the character that does not represent a floating-point number, into a long double type floating-point number. However, if neither an exponent nor a decimal point is found in the data to be converted, the compiler assumes that the decimal point comes next to the last digit in the string. In the area pointed by endptr, the function sets up a pointer to the first character that does not represent a floating-point number. If some characters that do not represent a floating-point number come before digits, the value of nptr is set. If endptr is NULL, nothing is set in this area.

 

strtol

Converts a number-representing string to a long type integer.

[Format]

#include <stdlib.h>
long strtol (const char *nptr, char **endptr, long base);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent an integer

base Radix of conversion (0 or 2 to 36)

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent an integer: 0
If the string pointed by nptr begins with a character that represents an integer: Converted data as a long type integer

Abnormal: If the converted data overflows: LONG_MAX or LONG_MIN depending on the sign of the string before conversion

[Remarks]

If the converted result overflows, errno is set.

The strtol function converts data, from the first digit up to the character before the first character that does not represent an integer, into a long type integer.

In the storage area pointed by endptr, the function sets up a pointer to the first character that does not represent an integer. If some characters that do not represent an integer come before the first digit, the value of nptr is set in this area. If endptr is NULL, nothing is set in this area.

If the value of base is 0, the rules described in section 3.1.3 (4), Integers, are observed at conversion. If the value of base is 2 to 36, it indicates the radix of conversion, where a (or A) to z (or Z) in the string to be converted correspond to numbers 10 to 35. If a character that is not smaller than the base value is found in the string to be converted, conversion stops immediately. A 0 after a sign is ignored at conversion. Similarly, 0x (or 0X) is ignored when base is 16.

 

strtoul

Converts a number-representing string to an unsigned long type integer.

[Format]

#include <stdlib.h>
unsigned long strtoul (const char *nptr, char **endptr, long base);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent an integer

base Radix of conversion (0 or 2 to 36)

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent an integer: 0
If the string pointed by nptr begins with a character that represents an integer: Converted data as an unsigned long type integer

Abnormal: If the converted data overflows: ULONG_MAX

[Remarks]

If the converted result overflows, errno is set.

The strtoul function converts data, from the first digit up to the character before the first character that does not represent an integer, into an unsigned long type integer.

In the storage area pointed by endptr, the function sets up a pointer to the first character that does not represent an integer. If some characters that do not represent an integer come before the first digit, the value of nptr is set in this area. If endptr is NULL, nothing is set in this area.

If the value of base is 0, the rules described in section 3.1.3 (4), Integers, are observed at conversion. If the value of base is 2 to 36, it indicates the radix of conversion, where a (or A) to z (or Z) in the string to be converted correspond to numbers 10 to 35. If a character that is not smaller than the base value is found in the string to be converted, conversion stops immediately. A 0 after a sign is ignored at conversion. Similarly, 0x (or 0X) is ignored when base is 16.

 

strtoll

Converts a number-representing string to a long long type integer.

[Format]

#include <stdlib.h>
long long strtoll (const char *nptr, char **endptr, long base);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent an integer

base Radix of conversion (0 or 2 to 36)

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent an integer: 0
If the string pointed by nptr begins with a character that represents an integer: Converted data as a long long type integer

Abnormal: If the converted data overflows: LLONG_MAX or LLONG_MIN depending on the sign of the string before conversion

[Remarks]

If the converted result overflows, errno is set.

The strtoll function converts data, from the first digit up to the character before the first character that does not represent an integer, into a long long type integer.

In the storage area pointed by endptr, the function sets up a pointer to the first character that does not represent an integer. If some characters that do not represent an integer come before the first digit, the value of nptr is set in this area. If endptr is NULL, nothing is set in this area.

If the value of base is 0, the rules described in section 3.1.3 (4), Integers, are observed at conversion. If the value of base is 2 to 36, it indicates the radix of conversion, where a (or A) to z (or Z) in the string to be converted correspond to numbers 10 to 35. If a character that is not smaller than the base value is found in the string to be converted, conversion stops immediately. A 0 after a sign is ignored at conversion. Similarly, 0x (or 0X) is ignored when base is 16.

 

strtoull

Converts a number-representing string to an unsigned long long type integer.

[Format]

#include <stdlib.h>
unsigned long long strtoull (const char *nptr, char **endptr, long base);

[Parameters]

nptr Pointer to a number-representing string to be converted

endptr Pointer to the storage area containing a pointer to the first character that does not represent an integer

base Radix of conversion (0 or 2 to 36)

[Return values]

Normal: If the string pointed by nptr begins with a character that does not represent an integer: 0
If the string pointed by nptr begins with a character that represents an integer: Converted data as an unsigned long long type integer

Abnormal: If the converted data overflows: ULLONG_MAX

[Remarks]

If the converted result overflows, errno is set.

The strtoull function converts data, from the first digit up to the character before the first character that does not represent an integer, into an unsigned long long type integer.

In the storage area pointed by endptr, the function sets up a pointer to the first character that does not represent an integer. If some characters that do not represent an integer come before the first digit, the value of nptr is set in this area. If endptr is NULL, nothing is set in this area.

If the value of base is 0, the rules described in section 3.1.3 (4), Integers, are observed at conversion. If the value of base is 2 to 36, it indicates the radix of conversion, where a (or A) to z (or Z) in the string to be converted correspond to numbers 10 to 35. If a character that is not smaller than the base value is found in the string to be converted, conversion stops immediately. A 0 after a sign is ignored at conversion. Similarly, 0x (or 0X) is ignored when base is 16.

 

rand

Generates a pseudo-random integer from 0 to RAND_MAX.

[Format]

#include <stdlib.h>
long rand (void);

[Return values]

Pseudo-random integer

 

srand

Sets an initial value of the pseudo-random number sequence generated by the rand function.

[Format]

#include <stdlib.h>
void srand (unsigned long seed);

[Parameters]

seed Initial value for pseudo-random number sequence generation

[Remarks]

The srand function sets up an initial value for pseudo-random number sequence generation of the rand function. If pseudo-random number sequence generation by the rand function is repeated and if the same initial value is set up again by the srand function, the same pseudo-random number sequence is repeated.

If the rand function is called before the srand function, 1 is set as the initial value for the pseudo-random number generation.

 

calloc

Allocates a storage area and clears all bits in the allocated storage area to 0.

[Format]

#include <stdlib.h>
void *calloc (size_t nelem, size_t elsize);

[Parameters]

nelem Number of elements

elsize Number of bytes occupied by a single element

[Return values]

Normal: Starting address of an allocated storage area

Abnormal: Storage allocation failed, or either of the parameter is 0: NULL

[Remarks]

The calloc function allocates as many storage units of size elsize (bytes) as the number specified by nelem. The function also clears all the bits in the allocated storage area to 0.

The CC-RX has a security facility for detecting illegal operations to storage areas. For details, refer to the -secure_malloc option in "2.5.4 Library Generator Options".

 

free

Releases the specified storage area.

[Format]

#include <stdlib.h>
void free (void *ptr);

[Parameters]

ptr Address of storage area to release

[Remarks]

The free function releases the storage area pointed by ptr, to enable reallocation for use. If ptr is NULL, the function carries out nothing.

If the storage area attempted to release was not allocated by the calloc, malloc, or realloc function, or when the area has already been released by the free or realloc function, correct operation is not guaranteed. Operation result of reference to a released storage area is also not guaranteed.

The CC-RX has a security facility for detecting illegal operations to storage areas. For details, refer to the -secure_malloc option in "2.5.4 Library Generator Options".

 

malloc

Allocates a storage area.

[Format]

#include <stdlib.h>
void *malloc (size_t size);

[Parameters]

size Size in number of bytes of storage area to allocate

[Return values]

Normal: Starting address of allocated storage area

Abnormal: Storage allocation failed, or size is 0: NULL

[Remarks]

The malloc function allocates a storage area of a specified number of bytes by size.

The CC-RX has a security facility for detecting illegal operations to storage areas. For details, refer to the -secure_malloc option in "2.5.4 Library Generator Options".

 

realloc

Changes the size of a storage area to a specified value.

[Format]

#include <stdlib.h>
void *realloc (void *ptr, size_t size);

[Parameters]

ptr Starting address of storage area to be changed

size Size of storage area in number of bytes after the change

[Return values]

Normal: Starting address of storage area whose size has been changed

Abnormal: Storage area allocation has failed, or size is 0: NULL

[Remarks]

The realloc function changes the size of the storage area specified by ptr to the number of bytes specified by size. If the newly allocated storage area is smaller than the old one, the contents are left unchanged up to the size of the newly allocated area.

When ptr is not a pointer to the storage area allocated by the calloc, malloc, or realloc function or when ptr is a pointer to the storage area released by the free or realloc function, operation is not guaranteed.

The CC-RX has a security facility for detecting illegal operations to storage areas. For details, refer to the -secure_malloc option in "2.5.4 Library Generator Options".

 

bsearch

Performs binary search.

[Format]

// C

#include <stdlib.h>
void *bsearch (const void *key, const void *base, size_t nmemb, size_t size, long (*compar)(const void *, const void *));

// C++/EC++

void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

[Parameters]

key Pointer to data to find

base Pointer to a table to be searched

nmemb Number of members to be searched

size Number of bytes of a member to be searched

compar Pointer to a function that performs comparison

[Return values]

If a matching member is found: Pointer to the matching member

If no matching member is found: NULL

[Remarks]

The bsearch function searches the table specified by base for a member that matches the data specified by key, by binary search method. The function that performs comparison should receive pointers p1 (first parameter) and p2 (second parameter) to two data items to compare, and return the result complying with the specification below.

*p1 < *p2: Returns a negative value.
*p1 == *p2: Returns 0.
*p1 > *p2: Returns a positive value.

Members to be searched must be placed in the ascending order.

 

qsort

Performs sorting.

[Format]

// C

#include <stdlib.h>
void qsort (const void *base, size_t nmemb, size_t size, long (*compar)(const void *, const void *));

// C++/EC++

#include <stdlib.h>

void qsort(const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

[Parameters]

base Pointer to the table to be sorted

nmemb Number of members to sort

size Number of bytes of a member to be sorted

compar Pointer to a function to perform comparison

[Remarks]

The qsort function sorts out data on the table pointed to by base. The data arrangement order is specified by the pointer to a function to perform comparison. This comparison function should receive pointers p1 (first parameter) and p2 (second parameter) as two data items to be compared, and return the result complying with the specification below.

*p1 < *p2: Returns a negative value.
*p1 == *p2: Returns 0.
*p1 > *p2: Returns a positive value.

 

abs

Calculates the absolute value of an int type integer.

[Format]

#include <stdlib.h>
long abs (long i);

[Parameters]

i Integer to calculate the absolute value

[Return values]

Absolute value of i

[Remarks]

If the resultant absolute value cannot be expressed as an int type integer, correct operation is not guaranteed.

 

div

Carries out division of int type integers and obtains the quotient and remainder.

[Format]

#include <stdlib.h>
div_t div (long numer, long denom);

[Parameters]

numer Dividend

denom Divisor

[Return values]

Quotient and remainder of division of numer by denom.

 

labs

Calculates the absolute value of a long type integer.

[Format]

#include <stdlib.h>
long labs (long j);

[Parameters]

j Integer to calculate the absolute value

[Return values]

Absolute value of j

[Remarks]

If the resultant absolute value cannot be expressed as a long type integer, correct operation is not guaranteed.

 

ldiv

Carries out division of long type integers and obtains the quotient and remainder.

[Format]

#include <stdlib.h>
ldiv_t ldiv (long numer, long denom);

[Parameters]

numer Dividend

denom Divisor

[Return values]

Quotient and remainder of division of numer by denom.

 

llabs

Calculates the absolute value of a long long type integer.

[Format]

#include <stdlib.h>
long long llabs (long long j);

[Parameters]

j Integer to calculate the absolute value

[Return values]

Absolute value of j

[Remarks]

If the resultant absolute value cannot be expressed as a long long type integer, correct operation is not guaranteed.

 

lldiv

Carries out division of long long type integers and obtains the quotient and remainder.

[Format]

#include <stdlib.h>
lldiv_t lldiv (long long numer, long long denom);

[Parameters]

numer Dividend

denom Divisor

[Return values]

Quotient and remainder of division of numer by denom.