itoa - C stdlib.h

C examples for stdlib.h:itoa

Type

function

From

<stdlib.h>

Description

Convert integer to string (non-standard function)

Prototype

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

Parameters

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.

Return Value

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

Portability

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.

Demo Code


#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;
}

Related Tutorials