strtol(3) Library Functions Manual strtol(3) strtol, strtoll, strtoq - (libc -lc) #include 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); glibc ( feature_test_macros(7)): strtoll(): _ISOC99_SOURCE || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE strtol() nptr base 2 36 0. ( isspace(3)) '+' '-' . base 16 "0x" "0X" 16 base 2 "0b" "0B" 2 base 10 () '0' 8 (). long . ( 10 'A' 10 'B' 11 'Z' 35.) endptr (NULL) base strtol() *endptr. strtol() nptr *endptr ( 0). *nptr '\0' **endptr '\0' . strtoll() strtol() long long. strtol() . strtol() LONG_MIN. strtol() LONG_MAX. errno ERANGE. strtoll() ( LLONG_MIN LLONG_MAX LONG_MIN LONG_MAX). errno . EINVAL ( C99) . ERANGE . errno EINVAL ( 0). attributes(7). +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ | | | | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ |strtol(), strtoll(), | | (locale) (MT-Safe) | |strtoq() | | | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ POSIX.1 "C" "POSIX" . BSD quad_t strtoq(const char *nptr, char **endptr, int base); . strtoll() strtol(). C23 POSIX.1-2024. strtol() POSIX.1-2001 C89 SVr4 4.3BSD. strtoll() POSIX.1-2001 C99. "0b" "0B" C23. glibc 2.38. ( POSIX.) strtol() 0 LONG_MAX LONG_MIN (LLONG_MAX LLONG_MIN strtoll()) errno 0 errno == ERANGE . errno = 0; n = strtol(s, &end, base); if (end == s) goto no_number; if ((errno == ERANGE && n == _Minof(long)) || n < min) goto too_low; if ((errno == ERANGE && n == _Maxof(long)) || n > max) goto too_high; base base . . errno = 0; strtol("0", NULL, base); if (errno == EINVAL) goto unsupported_base; . isspace(3) strtol(). strtol(). strtol() . () . ( atoi(3) 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 #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]\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); } atof(3), atoi(3), atol(3), strtod(3), strtoimax(3), strtoul(3) 3 . . : . 6.18 8 2026 strtol(3)