C Programming - Library Functions - Discussion
Discussion Forum : Library Functions - General Questions (Q.No. 5)
5.
Does there any function exist to convert the int or float to a string?
Answer: Option
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
Discussion:
6 comments Page 1 of 1.
Riya said:
1 decade ago
Please explain the function of 'sprintf()'
MOHD ILYAS said:
1 decade ago
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 ?
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:
1 decade ago
Can anybody explain what is 'radix' value ?
Giri said:
1 decade ago
Why using 10 what is radix value?
Vinni said:
1 decade ago
What is radix value?
Sachin said:
1 decade ago
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.
The itoa() function can convert an integer in decimal, octal or hexadecimal form to a string.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers