In C, why does
strtoul(argv[1])
just doesn't work? It looks like more parameters are needed but I can't prevent how long the number will be.
Thanks!
p.s. (argv[1] is properly setted).
In C, why does
strtoul(argv[1])
just doesn't work? It looks like more parameters are needed but I can't prevent how long the number will be.
Thanks!
p.s. (argv[1] is properly setted).
Because you're calling it with the wrong number of arguments. Try
strtoul(argv[1], 0, 0);
Or if you want to enforce base-10 only:
strtoul(argv[1], 0, 10);
Be sure you included <stdlib.h>
too!
The other parameters aren't for "how long the number will be". Read the manpage.
unsigned strtoul(char *s, char **endptr, int base);
endptr
should be either NULL
or the address of a char *
. If it's not NULL
the function sets the pointer to point to the first unused character in the string, so either a non-digit or the nul-termination.
base
specifies what base the number is in. It can be between 2 and 52 (I believe). You probably want 10. As a special case, base 0 checks for a prefix of 0x
for hexidecimal and 0
for octal (and none for decimal) and converts accordingly.