A
Arndt Jonasson
CBFalconer said:Its purpose is to confuse newbies and discourage proper testing for
input errors. Anyone with a modicum of knowledge would use
something in the strto*() family instead.
My habit has been to use 'atoi' when I need to convert a command line
argument to a number, and the number must be larger than 0. Then the
code looks like
if ((x = atoi(arg)) == 0)
usage();
Using 'strtol', it seems I have to do:
errno = 0;
x = strtol(arg, NULL, 10);
if (errno != 0)
usage();
though I do get the advantage that overflow is detected. If 0 is
allowed input, using 'strtol' also needs a check whether the input
string is empty, since it treats "" as valid (why was this considered
a good idea?). I have usually used 'sscanf' in this case.
My quibble doesn't amount to much, since I can't recommend a function
that doesn't detect malformed input before one that does, and the portion
of a program that deals with command line arguments is usually very small
compared to the rest.