Talk:Itoa

From Wikipedia, the free encyclopedia

Shouldn't there be a mention that itoa is not defined in ANSI-C, and that using this function may cause portability issues with your code since some compilers don't support it?

BrandonMintern 21:51, 14 May 2006 (UTC) Definitely. ANSI C is from 1989, the time that "the C programming language" didn't refer to one of the C standards (89,90,99)is long gone. At the very least point out that modern compilers name it _itoa (they use a prefix _ to make clear the distinction between standard and non-standard names 195.64.89.205 21:36, 13 June 2006 (UTC)

No. Just no. It is arguable enough whether functions should have their own articles *at all*. To make articles for non-standard functions is absurd. Where do we draw the line? Non-standard functions are not a part of the language, so why stop there? Why not list every single function that has ever been written in C? These articles should be deleted. Denis Kasak 00:19, 17 June 2006 (UTC)
I hardly agree, itoa may not be a standard function, but it is used, often. Often enough in my opinion to have it's own article. crashmatrix 09:23, 30 September 2006 (UTC)
Yes, the standard should have it. I just put down my idea on how it got there. Regarding the popularity of the K&R book, itoa first appeared there. So that may be why many people put it in stdlib.h - pietro gagliardi

The example function at freebookzone.com is very broken and probably shouldn't be referred to as an example --

  • it has parameters that differ from "those typically used" (I won't say "standard")
  • it uses malloc
  • it doesn't free one of the mallocs that is only used internally -- memory leak

[edit] Added implementations

Hello. Yesterday I added the original K&R implementation; today I made my own which works with radix too. Mine works (unlike my first attempt at writing sample C code for Wikipedia, a setjmp test). Here is a test program:

#include <stdio.h>
 
extern void itoa(int, char *, int);
 
int main(void)
{
        char buf[20];
 
        /* prints 20 */
        itoa(20, buf, 10);
        printf("%s\n", buf);
 
        /* prints 0xFF */
        itoa(0xFF, buf, 16);
        printf("0x%s\n", buf);
 
        /* prints -Z */
        itoa(-35, buf, 36);
        printf("%s\n", buf);
 
        return 0;
}

A problem with both my implementation and the K&R is if you try

#include <math.h>
 
/* ... */
 
void f(void)
{
    char buf[128];
 
    itoa(-(pow(2, sizeof (char) - 1), buf, 10);
}

You should get 0, but itoa returns -1. I don't know why :-) - PGSONIC 02:48, 10 July 2007 (UTC)

Update: I found a solution here: [1]. I don't know if the latter name mentioned changed the source, so I'm putting both of them in the mod. - PGSONIC 02:55, 10 July 2007 (UTC)
Update: Never mind - I had problems, so I reverted the edit. - PGSONIC 03:07, 10 July 2007 (UTC)

[edit] sprintf

Shouldnt we mention, that sprintf does the same, but for everey numeric(and many other) values? i/f/...toa is just specified versions of sprintf, and the standard doesnt even mention them... poor C programmers :) Tetra HUN (talk) 12:33, 19 March 2008 (UTC)