#include <stdlib.h> long strtol(const char *restrict nptr, char **restrict endptr, int base); long long strtoll(const char *restrict nptr, char **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
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; 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 uppercase or lowercase 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, 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.
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é).
Interface | Attribut | Valeur |
strtol(), strtoll(), strtoq() | Sécurité des threads | MT-Safe locale |
strtoll() : POSIX.1-2001, POSIX.1-2008, C99.
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().
$ ./a.out 123 strtol() returned 123 $ ./a.out ' 123' strtol() returned 123 $ ./a.out 123abc strtol() returned 123 Further characters after number: "abc" $ ./a.out 123abc 55 strtol: Invalid argument $ ./a.out '' No digits were found $ ./a.out 4000000000 strtol: Numerical result out of range
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; /* Pour distinguer la réussite/échec après l'appel */
val = strtol(str, &endptr, base);
/* Check for various possible errors. */
if (errno != 0) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "Pas de chiffre trouvé\n");
exit(EXIT_FAILURE);
}
/* If we got here, strtol() successfully parsed a number. */
printf("strtol() a renvoyé %ld\n", val);
if (*endptr != '\0') /* Not necessarily an error... */
printf("Further characters after number: \"%s\"\n", endptr);
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 à