How to force user to input a value in the type limit
I read this brilliant post by hammer about, how to get numbers safly using strtol() function.
My question is, how can I make sure the user DO NOT exceed the type limit??
If the user enter 91234567890 that will not be the desirable result becuase the value is bigger long maximum value.
if (fgets(buf, sizeof(buf), stdin) != NULL)
i = strtol(buf, &p, 10);
if (buf != '\n' && (*p == '\n' || *p == '\0'))
printf ("Valid number of %ld entered\n", i);
else printf ("Invalid number entered\n");
Is there anything I can do to avoid this problem?