.\" -*- coding: UTF-8 -*- .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" and Copyright 2006 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" 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. .\" %%%LICENSE_END .\" .\" 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 .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH STRTOL 3 "1 Noviembre 2020" GNU "Manual del Programador de Linux" .SH NOMBRE strtol, strtoll, strtoq \- convierten una cadena en un entero de tipo long .SH SINOPSIS .nf \fB#include \fP .PP \fBlong strtol(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP .PP \fBlong long strtoll(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP .fi .PP .RS -4 Requisitos de Macros de Prueba de Características para glibc (véase \fBfeature_test_macros\fP(7)): .RE .PP .ad l \fBstrtoll\fP(): .RS 4 _ISOC99_SOURCE || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE .RE .ad .SH DESCRIPCIÓN La función \fBstrtol\fP() convierte la parte inicial de la cadena de entrada \fInptr\fP en un valor entero de tipo long de acuerdo a la \fIbase\fP dada, que debe estar entre 2 y 36 ambos incluidos o ser el valor especial 0. .PP The string may begin with an arbitrary amount of white space (as determined by \fBisspace\fP(3)) followed by a single optional \(aq+\(aq or \(aq\-\(aq sign. If \fIbase\fP is zero or 16, the string may then include a "0x" or "0X" prefix, and the number will be read in base 16; otherwise, a zero \fIbase\fP is taken as 10 (decimal) unless the next character is \(aq0\(aq, in which case it is taken as 8 (octal). .PP The remainder of the string is converted to a \fIlong\fP 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 \(aqA\(aq in either uppercase or lowercase represents 10, \(aqB\(aq represents 11, and so forth, with \(aqZ\(aq representing 35.) .PP Si \fIendptr\fP no es NULL, \fBstrtol\fP() almacena la dirección del primer carácter no válido en \fI*endptr\fP. Si no hubiera dígitos en toda la cadena \fBstrtol\fP() almacena el valor original de \fInptr\fP en \fI*endptr\fP. (y devuelve 0). En particular, si \fI*nptr\fP es distinto de `\e0' pero \fI**endptr\fP es `\e0' a la vuelta, la cadena entera es válida. .PP La función \fBstrtoll\fP() hace el mismo trabajo que la función \fBstrtol\fP() pero devuelve un valor entero de tipo \fIlong long\fP. .SH "VALOR DEVUELTO" La función \fBstrtol\fP() devuelve el resultado de la conversión, a menos que el valor se desbordara por arriba o por abajo. Si ocurriera un desbordamiento inferior, \fBstrtol\fP() devuelve \fBLONG_MIN\fP. Si ocurriera un desbordamiento superior, \fBstrtol\fP() devuelve \fBLONG_MAX\fP. En ambos casos, \fIerrno\fP se establece a \fBERANGE\fP. Precisamente lo mismo se aplica a \fBstrtoll\fP() (con \fBLLONG_MIN\fP y \fBLLONG_MAX\fP en lugar de \fBLONG_MIN\fP y \fBLONG_MAX\fP). .SH ERRORES .TP \fBEINVAL\fP (no está en C99) La \fIbase\fP dada contiene un valor no soportado. .TP \fBERANGE\fP El valor resultante está fuera de rango. .PP La implementación puede poner también \fIerrno\fP a \fBEINVAL\fP en caso de que no se realice ninguna conversión (no se encuentren dígitos, y se devuelva 0). .SH ATRIBUTOS Para obtener una explicación de los términos usados en esta sección, véase \fBattributes\fP(7). .TS allbox; lbw29 lb lb l l l. Interfaz Atributo Valor T{ \fBstrtol\fP(), \fBstrtoll\fP(), \fBstrtoq\fP() T} Seguridad del hilo Configuración regional de multi\-hilo seguro .TE .SH "CONFORME A" \fBstrtol\fP(): POSIX.1\-2001, POSIX.1\-2008, C89, C99 SVr4, 4.3BSD. .PP \fBstrtoll\fP(): POSIX.1\-2001, POSIX.1\-2008, C99. .SH NOTAS Since \fBstrtol\fP() can legitimately return 0, \fBLONG_MAX\fP, or \fBLONG_MIN\fP (\fBLLONG_MAX\fP or \fBLLONG_MIN\fP for \fBstrtoll\fP()) on both success and failure, the calling program should set \fIerrno\fP to 0 before the call, and then determine if an error occurred by checking whether \fIerrno\fP has a nonzero value after the call. .PP According to POSIX.1, in locales other than "C" and "POSIX", these functions may accept other, implementation\-defined numeric strings. .PP BSD tiene también la función .PP .in +4n .EX \fBquad_t strtoq(const char *\fP\fInptr\fP\fB, char **\fP\fIendptr\fP\fB, int \fP\fIbase\fP\fB);\fP .EE .in .PP con una definición completamente análoga. Dependiendo del tamaño de palabra de la arquitectura actual, ésta puede ser equivalente a \fBstrtoll\fP() o a \fBstrtol\fP(). .SH EJEMPLOS The program shown below demonstrates the use of \fBstrtol\fP(). The first command\-line argument specifies a string from which \fBstrtol\fP() should parse a number. The second (optional) argument specifies the base to be used for the conversion. (This argument is converted to numeric form using \fBatoi\fP(3), a function that performs no error checking and has a simpler interface than \fBstrtol\fP().) Some examples of the results produced by this program are the following: .PP .in +4n .EX $\fB ./a.out 123\fP strtol() returned 123 $\fB ./a.out \(aq 123\(aq\fP strtol() returned 123 $\fB ./a.out 123abc\fP strtol() returned 123 Further characters after number: "abc" $\fB ./a.out 123abc 55\fP strtol: Invalid argument $\fB ./a.out \(aq\(aq\fP No digits were found $\fB ./a.out 4000000000\fP strtol: Numerical result out of range .EE .in .SS "Código fuente" \& .EX #include #include #include #include int main(int argc, char *argv[]) { int base; char *endptr, *str; long val; if (argc < 2) { fprintf(stderr, "Usage: %s str [base]\en", argv[0]); exit(EXIT_FAILURE); } str = argv[1]; base = (argc > 2) ? atoi(argv[2]) : 0; errno = 0; /* Para dstinguir éxito/error después de invocar */ val = strtol(str, &endptr, base); /* Check for various possible errors */ if (errno != 0) { perror("strtol"); exit(EXIT_FAILURE); } if (endptr == str) { fprintf(stderr, "No digits were found\en"); exit(EXIT_FAILURE); } /* If we got here, strtol() successfully parsed a number */ printf("strtol() returned %ld\en", val); if (*endptr != \(aq\e0\(aq) /* Not necessarily an error... */ printf("Further characters after number: \e"%s\e"\en", endptr); exit(EXIT_SUCCESS); } .EE .SH "VÉASE TAMBIÉN" \fBatof\fP(3), \fBatoi\fP(3), \fBatol\fP(3), \fBstrtod\fP(3), \fBstrtoimax\fP(3), \fBstrtoul\fP(3), .SH COLOFÓN Esta página es parte de la versión 5.10 del proyecto Linux \fIman\-pages\fP. Puede encontrar una descripción del proyecto, información sobre cómo informar errores y la última versión de esta página en \%https://www.kernel.org/doc/man\-pages/. .PP .SH TRADUCCIÓN La traducción al español de esta página del manual fue creada por Carlos Gomez Romero , Miguel Pérez Ibars y Marcos Fouces . .PP Esta traducción es documentación libre; lea la .UR https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 .UE o posterior con respecto a las condiciones de copyright. No existe NINGUNA RESPONSABILIDAD. .PP Si encuentra algún error en la traducción de esta página del manual, envíe un correo electrónico a .MT debian-l10n-spanish@lists.debian.org .ME .