Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Promoting unsigned long int to long int
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Ben Bacarisse, post: 3580973"] Exactly, though i was also talking about the if part as well. You had (corrected to avoid HUGE_VAL) if (errno == ERANGE) { if (l == ULONG_MAX) fprintf(...); } I'll say what I'd do below... And it is correct, but that does not mean that a return of zero means no conversion could be performed (it's and "if" not an "if and only if"). I think you are making this too complex. Only two things can go wrong with strtoul and they are easy to test for. If no conversion has been done, then the end pointer is set to the initial pointer passed. If a range error occurs, errno is set and ULONG_MAX is returned: unsigned long l; char *ep; errno = 0; if ((l = strtoul(num, &ep, 10)) == ULONG_MAX && errno == ERANGE) fprintf(stderr, "Number out of range.\n"); else if (ep == num) fprintf(stderr, "Invalid number (not converted).\n"); [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Promoting unsigned long int to long int
Top