C Programming/stdlib.h/itoa

The itoa (integer to ASCII) function is a widespread non-standard extension to the standard C programming language. It cannot be portably used, as it is not defined in any of the C language standards; however, compilers often provide it through the header  while in non-conforming mode, because it is a logical counterpart to the standard library function.



takes the integer input value  and converts it to a number in base. The resulting number (a sequence of base- digits) is written to the output buffer.

Depending on the implementation,  may return a pointer to the first character in , or may be designed so that passing a null   causes the function to return the length of the string that would have been written into a valid.

For converting a number to a string in base 8 (octal), 10 (decimal), or 16 (hexadecimal), a Standard-compliant alternative is to use the standard library function.

K&R implementation
The function  appeared in the first edition of Kernighan and Ritchie's The C Programming Language, on page 60. The second edition of The C Programming Language ("K&R2") contains the following implementation of, on page 64 [for Spanish editions go to page 47]. The book notes several issues with this implementation, including the fact that it does not correctly handle the most negative number &minus;2wordsize-1.

The function  used above is implemented two pages earlier:

Other appearances
An  function (and a similar function, , that converted a float to a string) was listed in the first-edition Unix manual. Unlike the versions given above, the Unix library version had an interface roughly equivalent to



and would invoke the callback routine  on each character in the output string, thus eliminating the need for a buffer big enough to hold the entire string.