Here is a minimal example with complete, correct error checking and diagnostic messages. Note that setting errno
to 0 is necessary for distinguishing range errors from valid strtoul()
outputs, this is an annoying quirk of the function.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fputs("usage: test NTHREAD\n", stderr);
exit(1);
}
char *e;
errno = 0;
unsigned long nthread = strtoul(argv[1], &e, 0);
if (!*argv[1] || *e) {
fputs("error: invalid NTHREAD\n", stderr);
exit(1);
}
if (nthread == (unsigned long) -1 && errno == ERANGE) {
fputs("error: NTHREAD out of range\n", stderr);
exit(1);
}
// Your code goes here
}