This program is to cast strings to uint64_t
types and get the errors if any.
It should output, in this case, 2 errors (overflow and negative number), none of which appear. Also, it doesn't properly cast one of the strings.
Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
int func(const char *buff) {
char *end;
int si;
errno = 0;
const uint64_t sl = strtoull(buff, &end, 10);
if (end == buff) {
fprintf(stderr, "%s: not a decimal number\n", buff);
} else if ('\0' != *end) {
fprintf(stderr, "%s: extra characters at end of input: %s\n", buff, end);
} else if ((sl < 0 || ULONG_MAX == sl) && ERANGE == errno) {
fprintf(stderr, "%s out of range of type uint64_t\n", buff);
} else if (sl > ULONG_MAX) {
fprintf(stderr, "%ld greater than ULONG_MAX\n", sl);
} else if (sl < 0) {
fprintf(stderr, "%ld negative\n", sl);
} else {
si = (int)sl;
}
return si;
}
int main()
{
printf("%d\n", func("123456789123465789"));
printf("%d\n", func("123456789123465789123465789"));
printf("%d\n", func("-1"));
return 0;
}