itoa in c

If you want to know more on itoa in c then read this blog post.

What is itoa in c programming?

itoa is a function which converts given integer into a string using a given base.

itoa terminates the string with null value.

This function returns a pointer to the resulting string.

This function uses a buffer to store the result.

This is not a c standard function. Hence you can use sprintf to write to a string.

This function is defined in stdlib.h header file.

Prototype of itoa

#include<stdlib.h>
char *itoa(int value, char *buffer,int radix);

Parameters of itoa in c

itoa function takes arguments of integer,buffer and radix.

Return value of itoa in c

itoa function returns a pointer to the resulting string.


Example program for itoa in c

Example 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
  int x=12345;
  char buffer[40];
  itoa(x,buffer,2);
  printf("Binary value is %d \n",buffer);
  return 0;
}

Code explanation

The input is passed to the itoa function which converts it to the string using the base value. The result is being printed onto the console.

Example 2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
  int x=12345;
  char buffer[40];
  itoa(x,buffer,16);
  printf("Hexadecimal value is %d \n",buffer);
  return 0;
}

Code explanation

The input is passed to the itoa function which converts it to the string using the base value. The result is being printed onto the console.


Difference between itoa and atoi in c

itoa function in c converts an integer to string using a given base. atoi in c is a function that converts the given string to an integer.

itoi function returns the pointer to the resulting string. atoi function returns the converted integral number. If the conversion is not possible then it returns zero.

itoi function takes three arguments. atoi function takes one argument.

itoa function terminates the process when it encounters a null value. atoi function terminates the process when it encounters a character which cannot be converted to an integer.

itoa in catoi in c
Converts integer to string using a given base.Converts the given string to an integer.
char *itoa(int value, char *buffer,int radix);int atoi(const char *string);
Takes three arguments.Takes one argument.
Difference between atoi and itoa in c programming

People also ask for

What is itoa function in c?

itoa function converts the given integer to an string. The result is placed in a buffer.

Which library function converts an integer long to a string?

itoa function converts the given integer long to a string. It stores the result in a buffer.

What does itoa stands for in c?

itoa stands for integer to ASCII. It is defined in stdlib.h header file.


People also read