strtoul

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

strtoul, strtoull, strtouq - 文字列を unsigned long int に変換する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <stdlib.h>

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

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

strtoull():

    _ISOC99_SOURCE
        || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
 

説明

strtoul() 関数は、 nptr の文字列の最初の部分を、 base を基数として unsigned long に変換する。 この base は 2 から 36 までの値 あるいは特別な意味を持つ値 0 でなければならない。

The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" or "0X" prefix, and the number will be read in base 16; if base is zero or 2, the string may then include a "0b" or "0B" prefix, and the number will be read in base 2; 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 uppercase or lowercase represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.)

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

strtoull() 関数は strtoul() 関数と同様だが、 unsigned long long 型の値を返す。  

返り値

The strtoul() function returns either the result of the conversion, or, if there was a leading minus sign, the negation of the result of the conversion represented as an unsigned value, unless the original (nonnegated) value would overflow; in the latter case, strtoul() returns ULONG_MAX and sets errno to ERANGE. Precisely the same holds for strtoull() (with ULLONG_MAX instead of ULONG_MAX).  

エラー

This function does not modify errno on success.
EINVAL
(C99 にはない) 指定された base がサポートされていない値である。
ERANGE
結果の値が範囲外である。

実装によっては、変換が行われなかった場合 (数字がなく、0 を返した場合)、 errno に EINVAL が設定される場合がある。  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
strtoul(), strtoull(), strtouq() Thread safetyMT-Safe locale
 

バージョン

"C" 以外のロケールの場合、その他の文字列も受け付けられるかもしれない。 (例えば、現在のロケールの 1000 毎の区切り文字に対応しているかもしれない。)

BSD には、

u_quad_t strtouq(const char *nptr, char **endptr, int base);

という完全に同様の定義を持つ関数がある。 使用中のアーキテクチャーのワード長次第であるが、この関数は strtoull() や stroul() と等価な場合もありえる。  

標準規格

C23, POSIX.1-2024.  

歴史

strtoul()
POSIX.1-2001, C89, SVr4.
strtoull()
POSIX.1-2001, C99.
"0b", "0B"
C23. glibc 2.38. (Not in POSIX.)
 

警告

strtoul () からは成功、失敗どちらの場合でも 0 や ULONG_MAX (strtoull() では ULLONG_MAX) が返る可能性があるので、 プログラムは関数を呼び出す前に errno を 0 に設定し、呼び出し後に errno が 0 以外の値かどうかを確認しエラーが発生したかどうかを判断する 必要がある。  

バグ

 

Signed numbers

Some negative values are considered valid input and are silently converted to unsigned long.  

White space

These functions silently accept leading whitespace.  

isalnum(3)

To reject white space and/or a sign, call isalnum(3) before strtoul().  

例

strtol(3) のマニュアルページの例を参照。 このページで説明した関数の使用方法も同様である。  

関連項目

a64l(3), atof(3), atoi(3), atol(3), strtod(3), strtol(3), strtoumax(3)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
属性
バージョン
標準規格
歴史
警告
バグ
Signed numbers
White space
isalnum(3)
例
関連項目

This document was created by man2html, using the manual pages.