avr-libc  2.0.0
Standard C library for AVR-GCC

AVR Libc Home Page

AVRs

AVR Libc Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

データ構造体 | マクロ | Typedefs | 関数 | 変数
<stdlib.h>: 汎用ユーティリティ

データ構造体

struct  div_t
 
struct  ldiv_t
 

マクロ

#define RAND_MAX   0x7FFF
 

型定義

typedef int(* __compar_fn_t) (const void *, const void *)
 

関数

void abort (void) __ATTR_NORETURN__
 
int abs (int __i)
 
long labs (long __i)
 
void * bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size, int(*__compar)(const void *, const void *))
 
div_t div (int __num, int __denom) __asm__("__divmodhi4")
 
ldiv_t ldiv (long __num, long __denom) __asm__("__divmodsi4")
 
void qsort (void *__base, size_t __nmemb, size_t __size, __compar_fn_t __compar)
 
long strtol (const char *__nptr, char **__endptr, int __base)
 
unsigned long strtoul (const char *__nptr, char **__endptr, int __base)
 
long atol (const char *__s) __ATTR_PURE__
 
int atoi (const char *__s) __ATTR_PURE__
 
void exit (int __status) __ATTR_NORETURN__
 
void * malloc (size_t __size) __ATTR_MALLOC__
 
void free (void *__ptr)
 
void * calloc (size_t __nele, size_t __size) __ATTR_MALLOC__
 
void * realloc (void *__ptr, size_t __size) __ATTR_MALLOC__
 
double strtod (const char *__nptr, char **__endptr)
 
double atof (const char *__nptr)
 
int rand (void)
 
void srand (unsigned int __seed)
 
int rand_r (unsigned long *__ctx)
 

変数

size_t __malloc_margin
 
char * __malloc_heap_start
 
char * __malloc_heap_end
 

非標準関数 (non-ISO C)

char * ltoa (long val, char *s, int radix)
 
char * utoa (unsigned int val, char *s, int radix)
 
char * ultoa (unsigned long val, char *s, int radix)
 
long random (void)
 
void srandom (unsigned long __seed)
 
long random_r (unsigned long *__ctx)
 
char * itoa (int val, char *s, int radix)
 
#define RANDOM_MAX   0x7FFFFFFF
 

double型引数の変換関数

この関数は、標準ライブラリ(libc.a)に含まれておらず、数学ライブラリ(libm.a)に含まれています。そのため、アプリケーションのリンク時に -lm オプションを指定する必要があります。

char * dtostre (double __val, char *__s, unsigned char __prec, unsigned char __flags)
 
char * dtostrf (double __val, signed char __width, unsigned char __prec, char *__s)
 
#define DTOSTR_ALWAYS_SIGN   0x01 /* put '+' or ' ' for positives */
 
#define DTOSTR_PLUS_SIGN   0x02 /* put '+' rather than ' ' */
 
#define DTOSTR_UPPERCASE   0x04 /* put 'E' rather 'e' */
 
#define EXIT_SUCCESS   0
 
#define EXIT_FAILURE   1
 

説明詳細

#include <stdlib.h>

このファイルは、ISO標準で定義されているいくつかの基本的なCマクロと関数に、AVR特有の拡張を追加して宣言しています。

マクロ定義説明

#define DTOSTR_ALWAYS_SIGN   0x01 /* put '+' or ' ' for positives */

dtostre()flags として使用できるビット値。

#define DTOSTR_PLUS_SIGN   0x02 /* put '+' rather than ' ' */

dtostre()flags として使用できるビット値。

#define DTOSTR_UPPERCASE   0x04 /* put 'E' rather 'e' */

dtostre()flags として使用できるビット値。

#define EXIT_FAILURE   1

exit()で終了に失敗。ゼロ以外の値として評価される。

#define EXIT_SUCCESS   0

exit()で終了に成功。0の値として評価される。

#define RAND_MAX   0x7FFF

rand()で生成できる最大値。

#define RANDOM_MAX   0x7FFFFFFF

random()で生成できる最大値。

型定義説明

typedef int(* __compar_fn_t) (const void *, const void *)

利便性のため、qsort()の比較関数の型を宣言。

関数説明

void abort ( void  )

abort()関数は、プログラム異常終了を引き起こします。実際は、割込みを抑制し、1を引数としてた_exit()関数にジャンプします。限られたAVR環境での実装として、無限ループへ入ることを効果的に抑止します。

int abs ( int  __i)

abs() 関数は、整数iの絶対値を計算します。

注意
The abs()関数とlabs()関数は、gccのビルトイン関数です。
double atof ( const char *  nptr)

atof()関数は、double形式で表現された文字列ポインタnptrの最初の部分を変換します。

これは、次の呼出しに等しいです。

1 strtod(nptr, (char **)0);
int atoi ( const char *  __s)

atoi()関数は、整数形式で表現された文字列ポインタsの最初の部分を変換します。また、次のコードとは対照的です。

1 (int)strtol(s, (char **)NULL, 10);

この関数は、オーバーフローを検出できない(errnoは更新されず、結果値も予想できない)が、少ないメモリ(フラッシュ・スタックとも)でより早く動作します。

long atol ( const char *  __s)

atol()関数は、整数形式で表現された文字列ポインタsの最初の部分を変換します。また、次のコードとは対照的です。

1 strtol(s, (char **)NULL, 10);

この関数は、オーバーフローを検出できない(errnoは更新されず、結果値も予想できない)が、少ないメモリ(フラッシュ・スタックとも)でより早く動作します。

void* bsearch ( const void *  __key,
const void *  __base,
size_t  __nmemb,
size_t  __size,
int(*)(const void *, const void *)  __compar 
)

bsearch() 関数は、先頭の要素をbaseが指すnmemb個のオブジェクトの配列内で、keyが指すオブジェクトに一致する要素を探索します。その配列の各要素の大きさをsizeで指定します。

配列の内容は、comparで示す比較関数に従い昇順でソートされている必要があります。compar関数は、keyオブジェクトへのポンタを第1引数として、配列要素へのポインタを第2引数として呼び出されます。その関数は、keyオブジェクトが配列要素より小さい、一致するまたは大きいとみなされると、それぞれ0より小さい、等しいまたは大きい整数を返さなければなりません。

bsearch()関数は、配列中の一致する要素へのポインタを返します。一致する要素がないときは、空(null)ポインタを返します。等しい要素が2つある場合、いずれの要素と一致するかは未規定となります。

void* calloc ( size_t  __nele,
size_t  __size 
)

大きさがsizeであるnele個の配列の領域を割り付けます。nele * sizeを引数としてmalloc()を呼び出すのと同じですが、割り付けられた領域を0で初期化します。

div_t div ( int  __num,
int  __denom 
)

div()関数は、num/denomの計算結果を、商及び剰余のquot(商)とrem(剰余)の2つのint型メンバを含むdiv_tの構造体で返します。

char* dtostre ( double  __val,
char *  __s,
unsigned char  __prec,
unsigned char  __flags 
)

The dtostre() function converts the double value passed in val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

Conversion is done in the format "[-]d.ddde±dd" where there is one digit before the decimal-point character and the number of digits after it is equal to the precision prec; if the precision is zero, no decimal-point character appears. If flags has the DTOSTRE_UPPERCASE bit set, the letter 'E' (rather than 'e' ) will be used to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is "00".

If flags has the DTOSTRE_ALWAYS_SIGN bit set, a space character will be placed into the leading position for positive numbers.

If flags has the DTOSTRE_PLUS_SIGN bit set, a plus sign will be used instead of a space character in this case.

The dtostre() function returns the pointer to the converted string s.

char* dtostrf ( double  __val,
signed char  __width,
unsigned char  __prec,
char *  __s 
)

The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.

Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the possible '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.

The dtostrf() function returns the pointer to the converted string s.

void exit ( int  __status)

The exit() function terminates the application. Since there is no environment to return to, status is ignored, and code execution will eventually reach an infinite loop, thereby effectively halting all code processing. Before entering the infinite loop, interrupts are globally disabled.

In a C++ context, global destructors will be called before halting execution.

void free ( void *  __ptr)

The free() function causes the allocated memory referenced by ptr to be made available for future allocations. If ptr is NULL, no action occurs.

char* itoa ( int  val,
char *  s,
int  radix 
)

Convert an integer to a string.

The function itoa() converts the integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

注意
The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.
警告
If the buffer is too small, you risk a buffer overflow.

Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after '9' will be the letter 'a'.

If radix is 10 and val is negative, a minus sign will be prepended.

The itoa() function returns the pointer passed as s.

long labs ( long  __i)

The labs() function computes the absolute value of the long integer i.

注意
The abs() and labs() functions are builtins of gcc.
ldiv_t ldiv ( long  __num,
long  __denom 
)

The ldiv() function computes the value num/denom and returns the quotient and remainder in a structure named ldiv_t that contains two long integer members named quot and rem.

char* ltoa ( long  val,
char *  s,
int  radix 
)

Convert a long integer to a string.

The function ltoa() converts the long integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

注意
The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (long int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.
警告
If the buffer is too small, you risk a buffer overflow.

Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after '9' will be the letter 'a'.

If radix is 10 and val is negative, a minus sign will be prepended.

The ltoa() function returns the pointer passed as s.

void* malloc ( size_t  __size)

The malloc() function allocates size bytes of memory. If malloc() fails, a NULL pointer is returned.

Note that malloc() does not initialize the returned memory to zero bytes.

See the chapter about malloc() usage for implementation details.

void qsort ( void *  __base,
size_t  __nmemb,
size_t  __size,
__compar_fn_t  __compar 
)

The qsort() function is a modified partition-exchange sort, or quicksort.

The qsort() function sorts an array of nmemb objects, the initial member of which is pointed to by base. The size of each object is specified by size. The contents of the array base are sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared.

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

int rand ( void  )

The rand() function computes a sequence of pseudo-random integers in the range of 0 to RAND_MAX (as defined by the header file <stdlib.h>).

The srand() function sets its argument seed as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

If no seed value is provided, the functions are automatically seeded with a value of 1.

In compliance with the C standard, these functions operate on int arguments. Since the underlying algorithm already uses 32-bit calculations, this causes a loss of precision. See random() for an alternate set of functions that retains full 32-bit precision.

int rand_r ( unsigned long *  __ctx)

Variant of rand() that stores the context in the user-supplied variable located at ctx instead of a static library variable so the function becomes re-entrant.

long random ( void  )

The random() function computes a sequence of pseudo-random integers in the range of 0 to RANDOM_MAX (as defined by the header file <stdlib.h>).

The srandom() function sets its argument seed as the seed for a new sequence of pseudo-random numbers to be returned by rand(). These sequences are repeatable by calling srandom() with the same seed value.

If no seed value is provided, the functions are automatically seeded with a value of 1.

long random_r ( unsigned long *  __ctx)

Variant of random() that stores the context in the user-supplied variable located at ctx instead of a static library variable so the function becomes re-entrant.

void* realloc ( void *  __ptr,
size_t  __size 
)

The realloc() function tries to change the size of the region allocated at ptr to the new size value. It returns a pointer to the new region. The returned pointer might be the same as the old pointer, or a pointer to a completely different region.

The contents of the returned region up to either the old or the new size value (whatever is less) will be identical to the contents of the old region, even in case a new region had to be allocated.

It is acceptable to pass ptr as NULL, in which case realloc() will behave identical to malloc().

If the new memory cannot be allocated, realloc() returns NULL, and the region at ptr will not be changed.

void srand ( unsigned int  __seed)

Pseudo-random number generator seeding; see rand().

void srandom ( unsigned long  __seed)

Pseudo-random number generator seeding; see random().

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

The strtod() function converts the initial portion of the string pointed to by nptr to double representation.

The expected form of the string is an optional plus ( '+' ) or minus sign ( '-' ) followed by a sequence of digits optionally containing a decimal-point character, optionally followed by an exponent. An exponent consists of an 'E' or 'e', followed by an optional plus or minus sign, followed by a sequence of digits.

Leading white-space characters in the string are skipped.

The strtod() function returns the converted value, if any.

If endptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr.

If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

If the correct value would cause overflow, plus or minus INFINITY is returned (according to the sign of the value), and ERANGE is stored in errno. If the correct value would cause underflow, zero is returned and ERANGE is stored in errno.

long strtol ( const char *  __nptr,
char **  __endptr,
int  __base 
)

The strtol() function converts the string in nptr to a long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).

The remainder of the string is converted to a long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter 'A' in either upper or lower case represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.)

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtol() stores the original value of nptr in endptr. (Thus, if *nptr is not '\0' but **endptr is '\0' on return, the entire string was valid.)

The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If no conversion could be performed, 0 is returned. If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped to LONG_MIN or LONG_MAX, respectively.

unsigned long strtoul ( const char *  __nptr,
char **  __endptr,
int  __base 
)

The strtoul() function converts the string in nptr to an unsigned long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).

The remainder of the string is converted to an unsigned long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter 'A' in either upper or lower case represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.)

If endptr is not NULL, strtoul() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtoul() stores the original value of nptr in endptr. (Thus, if *nptr is not '\0' but **endptr is '\0' on return, the entire string was valid.)

The strtoul() function return either the result of the conversion or, if there was a leading minus sign, the negation of the result of the conversion, unless the original (non-negated) value would overflow; in the latter case, strtoul() returns ULONG_MAX, and errno is set to ERANGE. If no conversion could be performed, 0 is returned.

char* ultoa ( unsigned long  val,
char *  s,
int  radix 
)

Convert an unsigned long integer to a string.

The function ultoa() converts the unsigned long integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

注意
The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (unsigned long int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.
警告
If the buffer is too small, you risk a buffer overflow.

Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after '9' will be the letter 'a'.

The ultoa() function returns the pointer passed as s.

char* utoa ( unsigned int  val,
char *  s,
int  radix 
)

Convert an unsigned integer to a string.

The function utoa() converts the unsigned integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

注意
The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (unsigned int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.
警告
If the buffer is too small, you risk a buffer overflow.

Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after '9' will be the letter 'a'.

The utoa() function returns the pointer passed as s.

変数説明

char* __malloc_heap_end
char* __malloc_heap_start
size_t __malloc_margin