J
Jonathan Burd
Greetings everyone,
Reading about the int64_t types added by C'99, I decided to implement
a 64-bit version of atoi for my own library. However, for
the test number provided, it isn't doing what it is supposed to do.
(I used GCC/MingW32 as the compiler with C'99 enabled. Oh, and there
is no overflow-checking in this code!) The code follows:
#include <ctype.h>
#ifdef __C99__
#include <stdint.h>
int64_t
__atoi64 (const char *numstr)
{
int64_t result = 0; /* stores the resulting number */
register int ch; /* current character */
int sign = '+'; /* sign of the number */
/* Try removing this and pass (const char*)NULL. */
if (NULL == numstr)
return 0;
/* skip leading whitespace */
while (isspace(*numstr))
++numstr;
/* get the sign. incl. '+' because it is valid! */
sign = *numstr;
if ('+' == sign || '-' == sign)
++numstr;
/* shift digits to left by one place each time
* and stick the new digit in.
*/
while ( (ch = *numstr) && isdigit(ch) )
{
result *= 10;
result += ch - '0';
++numstr;
}
/* return with sign. */
return (('-' == sign) ? -result : result);
}
#endif /* not __C99__ */
#ifdef TEST
#include <stdio.h>
int64_t __atoi64 (const char *numstr);
int
main (int argc, char **argv)
{
const char *numstr = "18726761288";
/* Implementation-specific:
* LONG_LONG_MAX as in limits.h = 9223372036854775807LL
* typedef long long int64_t;
*/
#ifdef __C99__
printf ("%s: %lld\n", numstr, __atoi64 (numstr));
#endif /* not __C99__ */
return 0;
}
#endif /* not TEST */
I wonder why this isn't working. Any help will be appreciated.
Nap time.
Regards,
Jonathan.
Reading about the int64_t types added by C'99, I decided to implement
a 64-bit version of atoi for my own library. However, for
the test number provided, it isn't doing what it is supposed to do.
(I used GCC/MingW32 as the compiler with C'99 enabled. Oh, and there
is no overflow-checking in this code!) The code follows:
#include <ctype.h>
#ifdef __C99__
#include <stdint.h>
int64_t
__atoi64 (const char *numstr)
{
int64_t result = 0; /* stores the resulting number */
register int ch; /* current character */
int sign = '+'; /* sign of the number */
/* Try removing this and pass (const char*)NULL. */
if (NULL == numstr)
return 0;
/* skip leading whitespace */
while (isspace(*numstr))
++numstr;
/* get the sign. incl. '+' because it is valid! */
sign = *numstr;
if ('+' == sign || '-' == sign)
++numstr;
/* shift digits to left by one place each time
* and stick the new digit in.
*/
while ( (ch = *numstr) && isdigit(ch) )
{
result *= 10;
result += ch - '0';
++numstr;
}
/* return with sign. */
return (('-' == sign) ? -result : result);
}
#endif /* not __C99__ */
#ifdef TEST
#include <stdio.h>
int64_t __atoi64 (const char *numstr);
int
main (int argc, char **argv)
{
const char *numstr = "18726761288";
/* Implementation-specific:
* LONG_LONG_MAX as in limits.h = 9223372036854775807LL
* typedef long long int64_t;
*/
#ifdef __C99__
printf ("%s: %lld\n", numstr, __atoi64 (numstr));
#endif /* not __C99__ */
return 0;
}
#endif /* not TEST */
I wonder why this isn't working. Any help will be appreciated.
Nap time.
Regards,
Jonathan.