top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

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

+5 votes
164 views
C: Does there any function to convert the int or float to a string?
posted Jan 21, 2014 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

4 Answers

+1 vote
 
Best answer

Yes, There is a function called sprintf() in header file stdio.h,

Example

#include<stdio.h>
int main(void)
{
    char str1[11];
    char str2[11];
    int x=143;
    float y=29.299;

    sprintf(str1,"%d",x);
    sprintf(str2,"%.3f",y);
    printf("str1=%s, str2=%s\n",str1,str2);

    return 0;
}

Output

str1=143, str2=29.299
answer Mar 13, 2016 by Ajay Kumar
0 votes

There is no function explicitly but you can use sprintf(str, "%f", float) or sprintf(str, "%d", integer)

answer Jan 22, 2014 by Sheetal Chauhan
0 votes

Yes, There is a function called "itoa()" which means Integer to ascii.

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

int main()
{
    int a=54325;
    char buffer[20];
    itoa(a,buffer,2);   // here 2 means binary
    printf("Binary value = %s\n", buffer);

    itoa(a,buffer,10);   // here 10 means decimal
    printf("Decimal value = %s\n", buffer);

    itoa(a,buffer,16);   // here 16 means Hexadecimal
    printf("Hexadecimal value = %s\n", buffer);
    return 0;
}

Output:

Binary value = 1101010000110101
Decimal value = 54325
Hexadecimal value = D435

You can write your own itoa function,
http://www.geeksforgeeks.org/implement-itoa/

answer Jul 3, 2015 by Chirag Gangdev
0 votes

Function
char *itoa(int value, char *string, int radix);

DESCRIPTION
The itoa() function constructs a string representation of an integer.

PARAMETERS
value:
Is the integer to be converted to string representation.

string:
Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes.

radix:
Is the base of the number; must be in the range 2 - 36.

answer Mar 10, 2016 by Ashish Kumar Khanna
Similar Questions
+2 votes

I want to pass a string over the network, After Coding and Decoding How will i make sure that this is the same string ?

+3 votes

For example:

Infix      Postfix 
------     -------- 
a+b          ab+ 
(a+b)*c      ab+c* 
a+b*c        abc*+ 
a*(b+c)-d/e  abc+*de/-
+1 vote

What type of conversion is not accepted in C and why?
a) char to int
b) float to char pointer
c) int to char
d) double to char

+1 vote

Is there any method in c++ to find the multiple position of a char in a string.
eg : Hi, How are you?
position of H : 1 and 5

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...