Javatpoint Logo
Javatpoint Logo

itoa Function in C

itoa () function is used to convert int data type to string data type in C language.

SYNTAX -

The string we place in the buffer pass must be large enough to hold the output. Since radix values can either be OCTAL(0 - 7), DECIMAL(0 - 9), or HEX(0 - 9, a - f). When the radix is DECIMAL, itoa() produces -

(void) sprintf(buffer, "%d", n);

Here, buffer returns character string.

When the radix is OCTAL, itoa() formats integer 'n' into an unsigned octal constant.

And when the radix is HEX, itoa() formats integer 'n' into unsigned hexadecimal constant.

The hexadecimal value will include lower case alphabets.

Return value -

The string pointer will be returned. When we pass a non-valid radix argument, the function will return NULL.

A standard-compliant alternative -

  • sprintf(str,"%d",value) - For conversion to decimal base.
  • sprintf(str,"%x",value) - For conversion to hexadecimal base.
  • sprintf(str,"%o",value) - For conversion to octal base.

Algorithm:

CODE -

Output

Input = 123456, base = 10, Buffer = 123456
Input = -2310, base = 10, Buffer = -2310 
Input = 10, base = 2, Buffer = 1010
itoa Function in C

Note: But we have to keep in mind that while we are compiling with gcc, we have to use the '-lm' flag to include the math library.

gcc -o test.out test.c -lm






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA