You need to pass the real address of the pointer if you want to check the error, so you can distinguish between 0 values ββarising from "0"
and similar values ββof 0 arising from "pqr"
:
char *endptr; errno = 0; long result = strtol(str, &endptr, 10); if (endptr == str) { // nothing parsed from the string, handle errors or exit } if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE) { // out of range, handle or exit } // all went fine, go on
source share