Newbie - itoa implementation
  Home FAQ Contact Sign in
comp.lang.c only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c Profile…
 Up
Newbie - itoa implementation         


Author: gareth.p.davies
Date: Aug 13, 2006 03:44

Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str[i];
}

Couple of notes on this.
Show full article (1.36Kb)
45 Comments
Re: Newbie - itoa implementation         


Author: pete
Date: Aug 13, 2006 04:38

gareth.p.davies@googlemail.com wrote:
>
> Hi.
>
> Just as a learning exercise, I've come up with an implementation of
> itoa() without using something like sprintf. However, I can't figure
> out how to extend it to deal with negative numbers - any help
> appreciated!
>
> char * itoa(int a)
> {
> #define BUFFSIZE 50
>
> static char str[BUFFSIZE];
>
> int i = sizeof(str) - 1;
>
> do
> {
> str[--i] = '0' + a % 10; ...
Show full article (1.53Kb)
4 Comments
Re: Newbie - itoa implementation         


Author: MQ
Date: Aug 13, 2006 04:59

gareth.p.davies@googlemail.com wrote:
> Hi.
>
> Just as a learning exercise, I've come up with an implementation of
> itoa() without using something like sprintf. However, I can't figure
> out how to extend it to deal with negative numbers - any help
> appreciated!
>
> char * itoa(int a)
> {
> #define BUFFSIZE 50
>
> static char str[BUFFSIZE];
>
> int i = sizeof(str) - 1;
>
> do
> {
> str[--i] = '0' + a % 10;
> } ...
Show full article (2.11Kb)
9 Comments
Re: Newbie - itoa implementation         


Author: pete
Date: Aug 13, 2006 05:23

MQ wrote:
>
> gareth.p.davies@googlemail.com wrote:
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> { ...
Show full article (1.97Kb)
5 Comments
Re: Newbie - itoa implementation         


Author: Harald van Dijk
Date: Aug 13, 2006 05:50

pete wrote:
> MQ wrote:
>>
>> gareth.p.davies@googlemail.com wrote:
>>> Hi.
>>>
>>> Just as a learning exercise, I've come up with an implementation of
>>> itoa() without using something like sprintf. However, I can't figure
>>> out how to extend it to deal with negative numbers - any help
>>> appreciated!
>>>
>>> char * itoa(int a)
>>> {
>>> #define BUFFSIZE 50
>>>
>>> static char str[BUFFSIZE];
>>>
>>> int i = sizeof(str) - 1;
>>>
>>> do ...
Show full article (2.30Kb)
4 Comments
Re: Newbie - itoa implementation         


Author: pemo
Date: Aug 13, 2006 06:05

MQ wrote:
> gareth.p.davies@googlemail.com wrote:
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> {
>> str[--i] = '0' + a % 10; ...
Show full article (2.74Kb)
no comments
Re: Newbie - itoa implementation         


Author: MQ
Date: Aug 13, 2006 06:14

pete wrote:
> However, you can copy the contents of the array
> to a distinct somewhere else,
> each time that the function is called.

sounds like a poor design to me. Simply passing the char array as a
parameter would be neater. This pattern exists for such library
functions as fread()

MQ
no comments
Re: Newbie - itoa implementation         


Author: gareth.p.davies
Date: Aug 13, 2006 06:20

pete wrote:
> gareth.p.davies@googlemail.com wrote:
>>
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> { ...
Show full article (2.23Kb)
13 Comments
Re: Newbie - itoa implementation         


Author: pemo
Date: Aug 13, 2006 06:23

MQ wrote:
> gareth.p.davies@googlemail.com wrote:
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> {
>> str[--i] = '0' + a % 10; ...
Show full article (2.35Kb)
no comments
Re: Newbie - itoa implementation         


Author: Ancient_Hacker
Date: Aug 13, 2006 06:47

Here's one that handles negative numbers:

#define BUFFSIZE 30

#define pc(c) ( 12345 == (*p99++ = c))

char * p99; char s99[BUFFSIZE];

void putd( int aa ){ if( aa > 9 ) putd( aa / 10 ); pc( (char) (aa %
10) + '0' ); }

char * ita( int a ) { p99 = s99; putd( a < 0 ? pc( '-' ) - a : a
);return s99 + pc( '\0' );
}

int main( int argc, char * argv[] )
{ char foo[1000]; int i; char * s; );
for( i = -123; i < 123 ; i ++ ) { s = ita( i ); printf( "%d = '%s'\n",
i, s ); }
gets_s( foo );
return 0;
}
no comments
1 2 3 4 5