How do I make sure that strtol() have returned successfully?

后端 未结 4 1181
天涯浪人
天涯浪人 2020-12-31 07:18

according documentation:

On success, the function returns the converted integral number as a long int value. If no valid conversion could be perfor

4条回答
  •  悲&欢浪女
    2020-12-31 07:25

    Since the accepted answer is not actually a correct way to check for failure.

    You should not check for errors by examining the return value of strtol, because the string might be a valid representation of 0l, LONG_MAX, or LONG_MIN. Instead, check whether tailptr points to what you expect after the number (e.g. '\0' if the string should end after the number). You also need to clear errno before the call and check it afterward, in case there was overflow.

提交回复
热议问题