Looking at forbidden information shows a way to use strtol in which the second argument is not NULL, allowing the programmer to validate the potential number without using anything from string.h. Adapting its example,
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf[] = "45fool";
char *p;
long int 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");
}
return(0);
}
/* my output
Invalid number entered
*/
This allows such things as validation of non-decimal values as well, alleviating the need to check for digit characters. For example,
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf[] = "123abc";
char *p;
long int i = strtol(buf, &p, 16);
if ( buf[0] != '\n' && (*p == '\n' || *p == '\0') )
{
printf ("Valid number of %ld entered\n", i);
}
else
{
printf ("Invalid number entered\n");
}
return(0);
}
/* my output
Valid number of 1194684 entered
*/
And the same could be done for smaller bases in which even some decimal characters would not be valid.