Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1251529
  • 博文数量: 264
  • 博客积分: 10772
  • 博客等级: 上将
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-25 11:54
文章分类

全部博文(264)

文章存档

2012年(4)

2011年(51)

2010年(31)

2009年(57)

2008年(51)

2007年(70)

分类: C/C++

2009-02-24 15:36:56

感觉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.
阅读(2806) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~