Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting float to char*

Tags:

How can I convert a float value to char* in C language?

like image 367
boom Avatar asked Jun 07 '10 10:06

boom


2 Answers

char buffer[64]; int ret = snprintf(buffer, sizeof buffer, "%f", myFloat);  if (ret < 0) {     return EXIT_FAILURE; } if (ret >= sizeof buffer) {     /* Result was truncated - resize the buffer and retry. } 

That will store the string representation of myFloat in myCharPointer. Make sure that the string is large enough to hold it, though.

snprintf is a better option than sprintf as it guarantees it will never write past the size of the buffer you supply in argument 2.

like image 107
Delan Azabani Avatar answered Nov 14 '22 05:11

Delan Azabani


char array[10]; sprintf(array, "%f", 3.123); 

sprintf: (from MSDN)

like image 45
aJ. Avatar answered Nov 14 '22 04:11

aJ.