Itoa
From Wikipedia, the free encyclopedia
- The correct title of this article is itoa. The initial letter is shown capitalized due to technical restrictions.
The itoa function is a widespread, but non-standard function of some libraries written in the C programming language. As such, it cannot be portably used, as it is not defined in any of the C language standards.
itoa(int integer, char *string, int radix)
Where integer
is the integer in which a number resides, string
is the string, represented by an array of characters and radix
is the base number in which the number will be represented, ranging from base 2 to 36
For converting a number to a string in bases 8 (octal), 10 (decimal), 16 (hexadecimal), a better, standards-compliant alternative way is to use the function sprintf
.
Contents |
[edit] History
In the 6th edition Unix reference manual, a tutorial on the C programming language existed. It is made available here by Dennis Ritchie, C's creator. It references a function ftoa, which does what itoa does but for floats. This is the first reference to a function of its kind. However, it is not implemented, only said to be in the compiler's library.
The function itoa first appeared in the book The C Programming Language as an implementation. This may be where it may have been popularized.
[edit] Example
Following is an example C program explaining a program using itoa.
/* itoa example in C */ #include <stdio.h> // Standard input/output header file, needed printf and scanf #include <stdlib.h> // This header file contains the itoa function int main(void) // The main program function { int i; // The variable where the number will be stored char buffer [50]; // The string where converted integer will be stored printf ("Enter a number: "); // Ask the user for a number scanf ("%d",&i); // And store it in the variable 'i' itoa (i,buffer,10); // Convert the integer to a base ten string printf ("decimal: %s\n",buffer); // And print it on the screen itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); // Same here, only hexadecimal itoa (i,buffer,2); // In binary printf ("binary: %s\n",buffer); return 0; // Terminate the program }
[edit] See also
[edit] External links
- A simple itoa function
- itoa example file
- itoa() implementations with performance tests
- lexical_cast - C++ alternative, part of the boost libraries