C ltoa() function

The ltoa() function is used to convert long data type to string data type. The syntax is as follows :
 char ltoa( long N, char *buffer, int base );
Where :
  • N is the long 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() {
  long num = 1000000;
  char buffer[50];

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

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

  ltoa(num, buffer, 16);  // here 16 means hexadecimal
  printf("Hexadecimal value : %s\n", buffer);
  return 0;
}
Output :

Binary value : 100001001000000

Decimal value : 16960

hexadecimal value : 4240

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

Next : C Language Tutorials List