[comp.os.msdos.programmer] TurboC++ Query about strtoul/strtol

tcs@router.jhuapl.edu (11/30/90)

Attempting to check some of the routines in the TC++ manual and I've come 
upon something strange (to me)

The code:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *string = "4294967295", *endptr;
    unsigned long lnumber;

    /* this is how it should be in the book */
    /* however, the example actually uses "strtol" */
    /* rather than "strtoul" */

    lnumber = strtoul(string, &endptr, 10);
    printf("string = %s  long = %lu\n", string, lnumber);

    return 0;
}

<end code>

If you replace strtoul with strtol, it will convert it correctly. So you 
could use strtoul or strtol and it will work either way (with TC++). But if 
you increment *string, the strtoul will make lnumber = MAX_UNSIGNED_LONG,
however, strtol will make lnumber = MAX_SIGNED_LONG. 

so:

Result (with strtoul and *string = "4294967296"):

string = 4294967296  long = 4294967295

Result (with strtol and *string = "4294967296"):

string = 4294967296  long = 2147483647

Shouldn't strtol wrap at any number that exceeds MAX_SIGNED_LONG ?

Is this a bug? No, I haven't called Borland yet, it's not a big deal or 
anything, just an observation and request for clarification. I mean, if 
it's supposed to work like this, then ... nevermind ...

Carl Schelin
tcs@mailer.jhuapl.edu