C Programming - Library Functions - Discussion

5. 

Does there any function exist to convert the int or float to a string?

[A]. Yes
[B]. No

Answer: Option A

Explanation:

1. itoa() converts an integer to a string.
2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to string type.

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
   int   num1 = 12345;
   float num2 = 5.12;
   char str1[20];
   char str2[20];

   itoa(num1, str1, 10); /* 10 radix value */
   printf("integer = %d string = %s \n", num1, str1);

   sprintf(str2, "%f", num2);
   printf("float = %f string = %s", num2, str2);

   return 0;
}

// Output:
// integer = 12345 string = 12345
// float = 5.120000 string = 5.120000


Riya said: (Apr 29, 2011)  
Please explain the function of 'sprintf()'

Mohd Ilyas said: (Jun 29, 2011)  
In the above program I am getting error :In function `main':
undefined reference to `itoa'

but when I commented the line 5 & 6,I got the output for4 only 7,8 line i.e: float = 5.120000 string = 5.120000

Can any body rectify this error ?

Pat said: (Jun 17, 2013)  
Can anybody explain what is 'radix' value ?

Giri said: (Mar 15, 2014)  
Why using 10 what is radix value?

Vinni said: (Aug 23, 2014)  
What is radix value?

Sachin said: (Dec 11, 2014)  
itoa() takes the integer input value input and converts it to a number in base radix. The resulting number a sequence of base-radix digits.

The itoa() function can convert an integer in decimal, octal or hexadecimal form to a string.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.