C itoa() function

The itoa() function is used to converts the int data type to string data type. The syntax of itoa() is as follows :
 char * itoa( int value, char *buffer, int base)
Where :
  • value is the int value we want to convert.
  • buffer is the string data type variable ewhere the converted string is stored.
  • base specify the conversion base. for example if base is 2 then it will convert the integer into its binary compatible string or if base is 16, then it will create hexadecimal converted string form of integer number. If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, value is always considered unsigned. The value of base is ranges from 2 to 36.
Example :
#include <stdio.h>
#include <stdlib.h>

int main() {
  int num = 1575;
  char buffer[30];

  itoa(num, buffer, 2); // here 2 means binary
  printf("Binary value = %s\n\n", buffer);

  itoa(num, buffer, 10);  // here 10 means decimal
  printf("Decimal value = %s\n\n", buffer);

  itoa(num, buffer, 16);  // here 16 means hexadecimal
  printf("Hexadecimal value = %s\n\n", buffer);

  return 0;
Output :

Binary value : 11000100111

Decimal value : 1575

Hexadecimal value : 627

Note : The function itoa() is not available in latest gcc compiler. The above example is compiled on Turbo C compiler.

Next : C Language Tutorials List