感觉strtoul这个函数很烦
主要是在对返回值的检查上
网上有一个例子
#include
#include
#include
#include
#define LENGTH 40
main()
{
char String[LENGTH], *endptr;
long int retval;
(void)setlocale(LC_ALL, "");
if (fgets(String, LENGTH, stdin) != NULL) {
errno = 0;
retval = strtol ( String, &endptr, 0 );
if (retval == 0 && (errno != 0
|| String == endptr)) {
/* No conversion could be performed */
printf("No conversion performed\n");
} else if (errno !=0 && (retval == LONG_MAX
|| retval == LONG_MIN)) {
/* Error handling */
} else {
/* retval contains long integer */
printf("Integer in decimal is %d\n", retval);
}
}
}
The strtoul() function shall not change the setting of errno if successful.
Since 0, {ULONG_MAX}, and {ULLONG_MAX} are returned on error and are also valid returns on success, an application wishing to check for error situations should set errno to 0, then call strtoul() or strtoull(), then check errno.
阅读(2827) | 评论(0) | 转发(0) |