1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" References consulted:
.\" Linux libc source code
.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
.\" 386BSD man pages
.\" Modified Sun Jul 25 10:53:39 1993 by Rik Faith (faith@cs.unc.edu)
.\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
.\"
.\" Japanese Version Copyright (c) 1999 AKAMATSU, Kazuo
.\" all rights reserved.
.\" Translated Thu Jan 9 21:00:00 JST 1999
.\" by AKAMATSU, Kazuo
.\" Updated Sun Apr 8 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
.\" Updated Sat Mar 23 JST 2002 by Kentaro Shirakata <argrath@ub32.org>
.\" Updated & Modified 2006-07-20,
.\" Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
.\"
.\" WORD: base
.\"
.TH STRTOL 3 2007-07-26 "GNU" "Linux Programmer's Manual"
.SH ̾
strtol, strtoll, strtoq \- ʸ long int Ѵ
.SH
.nf
.B #include <stdlib.h>
.sp
.BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
.sp
.BI "long long int strtoll(const char *" nptr ", char **" endptr \
", int " base );
.fi
.sp
.in -4n
glibc εǽޥ
.RB ( feature_test_macros (7)
):
.in
.sp
.ad l
.BR strtoll ():
XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE; or
.I cc\ -std=c99
.ad b
.SH
.BR strtol ()
ؿϡ \fInptr\fP ʸκǽʬ
\fIbase\fP Ȥ long int Ѵ롣
\fIbase\fP 2 36 ޤǤ
뤤̤ʰ̣ 0 ǤʤФʤʤ
.PP
ʸƬˤϡǤդοζäƤ褯 (
.BR isspace (3)
Ƚꤵ)ޤľˤ \(aq+\(aq \(aq\-\(aq
椬äƤ褤
\fIbase\fP 0 ޤ 16 ξˤϡʸƬ "0x" ֤Ȥ
Ǥξˤʸ 16ʿȤư롣
ʳʸ \fIbase\fP 0 ξϡ
ʸ \(aq0\(aq ǻϤޤȤ 8ʿȤơ
ʳΤȤ 10ʿȤư롣
.PP
ɽʸ
.I long int
Ѵ뤬Ф
ͭǤʤ줿ѴϽλ롣(11ʿʾǤ \(aqA\(aq
ʸʸ˴ؤ餺 10 ɽ \(aqB\(aq 11 ɽ
ʲƱͤˡ \(aqZ\(aq 35 ɽ)
.PP
\fIendptr\fP ̥ (NULL) Ǥʤϡǽ˸줿ʸ
.BR strtol ()
ˤä \fI*endptr\fP ¸Ƥ롣
ʸͭʿҤȤĤʤС
.BR strtol ()
\fInptr\fP
θͤ \fI*endptr\fP ( 0 ֤)
äˡ\fI*nptr\fP \(aq\\0\(aq ʳǡ֤줿 \fI**endptr\fP
\(aq\\0\(aq ʤСʸΤͭäȤˤʤ롣
.PP
.BR strtoll ()
ؿ
.BR strtol ()
Ʊͤlong long int ֤ͤ
.SH ֤
ե⥪Сեⵯʤä硢
.BR strtol ()
ؿ
Ѵ줿֤ͤСեˤ
.B LONG_MAX
֤ꡢ
եˤ
.B LONG_MIN
֤롣Сե
եΤξˤ
ѿ \fIerrno\fP ˤ
.B ERANGE
ꤵ롣
.BR strtoll ()
ƱͤǤ뤬
.B LONG_MIN
.B LONG_MAX
.B LLONG_MIN
.B LLONG_MAX
֤롣
.SH 顼
.TP
.B EINVAL
(C99 ˤϤʤ)
.I base
бƤʤͤǤ롣
.TP
.B ERANGE
̤ͤϰϳǤ롣
.LP
ˤäƤϡѴԤʤä (ʤ0 ֤)
\fIerrno\fP \fBEINVAL\fP ꤵ礬롣
.SH
.BR strtol ()
SVr4, 4.3BSD, C89, C99 POSIX.1-2001 ˽Ƥ롣
.BR strtoll ()
C99 POSIX.1-2001 ˽Ƥ롣
.SH
.B strtol ()
ԤɤξǤ
0,
.BR LONG_MAX ,
.B LONG_MIN
.RB ( strtoll ()
Ǥ
.BR LLONG_MAX ,
.BR LLONG_MIN )
֤ǽΤǡ
ץϴؿƤӽФ
.I errno
0 ꤷƤӽФ
.I errno
0 ʳͤɤǧ顼ȯɤȽǤ
ɬפ롣
"C" ʳΥξ硢¾ʸդ뤫⤷ʤ
(㤨СߤΥ 1000 ζڤʸݡȤƤ뤫⤷ʤ)
.LP
BSD ˤϡ
.sp
.in +4n
.nf
.BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
.sp
.in
.fi
ȤƱͤĴؿ롣
ΥƥΥĹǤ뤬δؿ
.BR strtoll ()
.BR strtol ()
Ȥʤ뤳Ȥ⤢롣
.SH
ʲΥץ
.BR strtol ()
λǤ롣
ǽΥޥɥ饤ˤ
.BR strtol ()
ȤƲ᤹ʸꤹ롣
(άǽ) ܤΰˤ
Ѵ˻Ѥꤹ
(ΰ
.BR atoi (3)
ȤäƿͤѴ롣
.BR atoi (3)
.BR strtol ()
ñʥեĴؿǡ
Ǥϥ顼åϹԤʤ)
Υץμ¹Ĥʲ˼:
.in +4n
.nf
.RB "$" " ./a.out 123"
strtol() returned 123
.RB "$" " ./a.out \(aq 123\(aq"
strtol() returned 123
.RB "$" " ./a.out 123abc"
strtol() returned 123
Further characters after number: abc
.RB "$" " ./a.out 123abc 55"
strtol: Invalid argument
.RB "$" " ./a.out \(aq\(aq"
No digits were found
.RB "$" " ./a.out 4000000000"
strtol: Numerical result out of range
.fi
.in
.SS ץΥ
\&
.nf
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
int base;
char *endptr, *str;
long val;
if (argc < 2) {
fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
exit(EXIT_FAILURE);
}
str = argv[1];
base = (argc > 2) ? atoi(argv[2]) : 10;
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, base);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "No digits were found\\n");
exit(EXIT_FAILURE);
}
/* If we got here, strtol() successfully parsed a number */
printf("strtol() returned %ld\\n", val);
if (*endptr != \(aq\\0\(aq) /* Not necessarily an error... */
printf("Further characters after number: %s\\n", endptr);
exit(EXIT_SUCCESS);
}
.fi
.SH Ϣ
.BR atof (3),
.BR atoi (3),
.BR atol (3),
.BR strtod (3),
.BR strtoul (3)
|