Sunday, August 2, 2009

How in C programming,we can assign a float value to a char value?

like if i calculated the any value ,whose result is ,say,17.346 then how can i display it in a function outtextxy() ???

How in C programming,we can assign a float value to a char value?
Can't be done - there aren't enough bits in a char for a float, a float (usually) takes 32 bits. If your variable is an int you can do it with a cast, but that is extremely discouraged.





Actually, what you mean is not char but char *, address of an asciz string.





What you need to do is use something like sprintf() to make a character string of the value, and then use that.
Reply:You can't do a float to a char but you can to a string.





char buf[80];


float fred=2.345;





sprintf(buf, "%f", fred);





buf now = "2.345"





use "%x.nf" where x = size and n is no of decimal points for instance





sprintf(buf, "£.2f", fred);





will produce a monitory output.


No comments:

Post a Comment