|
From: Ioannis V. <iv...@em...> - 2003-04-02 21:19:49
|
Ok, in order to be complete (with what I consider as an itoa() without =
any
specific constraint requirements) I repost a complete itoa(), with the =
few
bugs fixed. The implementation was influenced by the spirit "(Real
programmers) Don't comment their code. If it was hard to write, it =
should be
hard to
understand and even harder to modify." See the attached file.
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
char *itoa(int i)
{
size_t Length;
char *p;
int temp=3Di;
=20
int sign=3Dtemp<0?-1:1;
=20
if(!sign)
{
p=3Dmalloc(2);
=09
if(!p)
return NULL;
=20
p[0]=3D'0', p[1]=3D0;
return p;
}
=20
temp=3Dsign*i;
for(Length=3D0; temp/=3D10; Length++)
;
p=3Dmalloc(Length+1+!sign=3D=3D1);
if(!p)
return NULL;
sprintf(p, "%d", i);
=20
return p;
}
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|