C Data Type Functions - C itoa






Convert integer to string (non-standard function)

Prototype

char * itoa ( int value, char * str, int base );

Parameter

This function has the following parameter.

value
Value to be converted to a string.
str
Array where to store the resulting null-terminated string.
base
Numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.

Return

A pointer to the resulting null-terminated string, same as parameter str.

Example


#include <stdio.h>
#include <stdlib.h>
//from w  ww . j  a va 2 s.  c  om
int main (){
  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;
} 

       

The code above generates the following result.