C examples for stdlib.h:itoa
function
<stdlib.h>
Convert integer to string (non-standard function)
char * itoa ( int value, char * str, int base );
Parameter | Description |
---|---|
value | Value to be converted. |
str | string to store the converted int. |
base | Numerical base used to represent the value as a string, between 2 and 36. |
A pointer to the resulting null-terminated string, same as parameter str.
A standard-compliant alternative for some cases may be sprintf:
sprintf(str,"%d",value) converts to decimal base. sprintf(str,"%x",value) converts to hexadecimal base. sprintf(str,"%o",value) converts to octal base.
#include <stdio.h> #include <stdlib.h> int main ()/*w w w . j a va 2s . c o m*/ { int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,10); printf ("decimal: %s\n",buffer); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); itoa (i,buffer,2); printf ("binary: %s\n",buffer); return 0; }