Man page - strtoll(3)

Packages contains this manual

Available languages:

en fr nl ja

Manual

strtol

NOM
BIBLIOTHÈQUE
SYNOPSIS
DESCRIPTION
VALEUR RENVOYÉE
ERREURS
ATTRIBUTS
VERSIONS
STANDARDS
HISTORIQUE
CAVEATS
EXEMPLES
Source du programme
VOIR AUSSI
TRADUCTION

NOM

strtol, strtoll, strtoq - Convertir une chaĂźne en un entier long

BIBLIOTHÈQUE

BibliothĂšque C standard ( libc , -lc )

SYNOPSIS

#include <stdlib.h>

long strtol(const char *restrict nptr ,
char **_Nullable restrict
endptr , int base );
long long strtoll(const char *restrict
nptr ,
char **_Nullable restrict
endptr , int base );

Exigences de macros de test de fonctionnalités pour la glibc (consulter feature_test_macros (7)) :

strtoll () :
_ISOC99_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

DESCRIPTION

La fonction strtol () convertit la partie initiale de la chaĂźne nptr en un entier long en fonction de l’argument base , qui doit ĂȘtre dans l’intervalle 2 Ă  36 (bornes comprises), ou avoir la valeur spĂ©ciale 0 .

La chaĂźne peut commencer par un nombre quelconque d’espaces (tels que dĂ©finis par isspace (3)) suivi d’un Ă©ventuel signe « + » ou « - ». Si base vaut 0 ou 16 , la chaĂźne peut inclure un prĂ©fixe « 0x » ou « 0X » et le nombre sera interprĂ©tĂ© en base 16 . Sinon, une base valant zĂ©ro est interprĂ©tĂ©e comme 10 (base dĂ©cimale) sauf si le caractĂšre suivant est « 0 », auquel cas la base est 8 (base octale).

Le reste de la chaĂźne est converti en une valeur long , en s’arrĂȘtant au premier caractĂšre qui ne soit pas un chiffre autorisĂ© dans cette base. Dans les bases supĂ©rieures Ă  10 , la lettre « A » (majuscule ou minuscule) reprĂ©sente 10 , « B » reprĂ©sente 11 , et ainsi de suite jusqu’à « Z » reprĂ©sentant 35 .

If endptr is not NULL, and the base is supported, strtol () stores the address of the first invalid character in *endptr . If there were no digits at all, strtol () 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.

La fonction strtoll () fonctionne comme strtol () mais renvoie une valeur entiĂšre de type long long .

VALEUR RENVOYÉE

La fonction strtol () renvoie le rĂ©sultat de la conversion, Ă  moins qu’un dĂ©bordement supĂ©rieur (overflow) ou infĂ©rieur (underflow) se produise. Si un dĂ©passement infĂ©rieur se produit, strtol () renvoie LONG_MIN . Si un dĂ©passement supĂ©rieur se produit, strtol () renvoie LONG_MAX . Dans les deux cas, errno contient le code d’erreur ERANGE . La mĂȘme chose est vraie pour strtoll () avec LLONG_MIN et LLONG_MAX Ă  la place de LONG_MIN et LONG_MAX .

ERREURS

This function does not modify errno on success.

EINVAL

(pas dans C99) La base indiquĂ©e n’est pas prise en charge.

ERANGE

La valeur retournée est hors limites.

L’implĂ©mentation peut aussi mettre errno Ă  EINVAL si aucune conversion n’a Ă©tĂ© rĂ©alisĂ©e (pas de chiffres trouvĂ©s, et 0 renvoyĂ©).

ATTRIBUTS

Pour une explication des termes utilisés dans cette section, consulter attributes (7).

Image grohtml-3895001-1.png

VERSIONS

POSIX.1 spĂ©cifie que dans le cas de locales autres que « C » et « POSIX », ces fonctions peuvent accepter des chaĂźnes numĂ©riques propres Ă  l’implĂ©mentation.

BSD a aussi

quad_t strtoq(const char * nptr , char ** endptr , int base );

avec une dĂ©finition exactement analogue. Suivant l’architecture, cela peut ĂȘtre Ă©quivalent Ă  strtoll () ou strtol ().

STANDARDS

C11, POSIX.1-2008.

HISTORIQUE

strtol ()

POSIX.1-2001, C89, SVr4, 4.3BSD.

strtoll ()

POSIX.1-2001, C99.

CAVEATS

Since strtol () can legitimately return 0, LONG_MAX , or LONG_MIN ( LLONG_MAX or LLONG_MIN for strtoll ()) on both success and failure, the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno == ERANGE after the call.

If the base needs to be tested, it should be tested in a call where the string is known to succeed. Otherwise, it’s impossible to portably differentiate the errors.

errno = 0;
strtol("0", NULL, base);
if (errno == EINVAL)
goto unsupported_base;

EXEMPLES

Le programme suivant montre l’utilisation de strtol (). Le premier argument de la ligne de commande spĂ©cifie une chaĂźne dans laquelle strtol () analysera un nombre. Le second argument, optionnel, spĂ©cifie la base Ă  utiliser pour la conversion. (Cet argument est converti sous forme numĂ©rique avec atoi (3), une fonction qui n’effectue aucune vĂ©rification d’erreur et qui a une interface plus simple que strtol ()). Voici plusieurs exemples de rĂ©sultats produits par ce programme :

$ ./a.out 123
strtol() a renvoyé 123
$ ./a.out ' 123'
strtol() a renvoyé 123
$ ./a.out 123abc
strtol() a renvoyé 123
CaractÚres trouvés aprÚs le nombre : abc
$ ./a.out 123abc 55
strtol: Argument non valable
$ ./a.out ''
Pas de chiffre trouvé
$ ./a.out 4000000000
strtol: Résultat numérique hors intervalle

Source du programme

#include <errno.h>
#include <stdio.h>
#include <stdlib.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]) : 0;
errno = 0; /* To distinguish success/failure after call */
strtol("0", NULL, base);
if (errno == EINVAL) {
perror("strtol");
exit(EXIT_FAILURE);
}
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, base);
/* Check for various possible errors. */
if (errno == ERANGE) {
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 != '\0') /* Not necessarily an error... */
printf("Further characters after number: \"%s\"\n", endptr);
exit(EXIT_SUCCESS);
}

VOIR AUSSI

atof (3), atoi (3), atol (3), strtod (3), strtoimax (3), strtoul (3)

TRADUCTION

La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org> et Grégoire Scano <gregoire.scano@malloc.fr>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n’y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org .