Hi guys,
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.Code:if (fgets(buf, sizeof(buf), stdin) != NULL) { i = strtol(buf, &p, 10); if (buf[0] != '\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?
Many thanks....



LinkBack URL
About LinkBacks



