I have the following code:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
void main(void)
{
int data;
char * tmp;
data = strtol("23ef23",&tmp,10);
printf("%d",errno);
getchar();
}
output is 0 …
why?
i am using visual studio 2010 C++
code must be C89 compatible.
strtolonly setserrnofor overflow conditions, not to indicate parsing failures. For that purpose, you have to check the value of the end pointer, but you need to store a pointer to the original string:I think the only required error values are for underflow and overflow.
Conversely, if the entire string has been consumed in the conversion, you have
*endptr = '\0', which may be an additional thing you might want to check.