Article View: comp.os.msdos.programmer
Article #3215TurboC++ Query about strtoul/strtol
From: tcs
Date: Thu, 29 Nov 1990 21:26
Date: Thu, 29 Nov 1990 21:26
49 lines
1823 bytes
1823 bytes
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
Technical Details
Message-ID:
<0094071F.506EDB00@router.jhuapl.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!TCS@router.jhuapl.eduShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!TCS@router.jhuapl.edu From: tcs@router.jhuapl.edu Newsgroups: comp.os.msdos.programmer Subject: TurboC++ Query about strtoul/strtol Message-ID: <0094071F.506EDB00@router.jhuapl.edu> Date: 29 Nov 90 21:26:02 GMT Sender: news@aplcen.apl.jhu.edu (USENET News System) Reply-To: tcs@router.jhuapl.edu Organization: Johns Hopkins University/APL Lines: 49