AnsweredAssumed Answered

sprintf saves garbage values into character array

Question asked by Swapnil V on Oct 29, 2014
Latest reply on Nov 7, 2014 by Matthew Kendall


I have mqx 4.0.1 installled on my device. I am using its web server.

When sending the data from device to web browser I am using sprintf at several places in device side C code.


I am using sprintf to copy float values into a character array as below:


CHAR MyArray[50]={0};


FLOAT f_Value=0.0;


sprintf(MyArray, "%5.3f", f_Value);


The float value is first read into variable "f_Value" and then I am using sprintf() as mentioned above to copy the float value into buffer "MyArray".


The value that is read into variable "f_Value" is correct.

But when I use sprintf() to copy it to "MyArray" then sometimes a garbage value(18434449246735240000000.000) gets stored in "MyArray".

Sometimes if  variable "f_Value" is having value say 100.000 into it; then after copying it to "MyArray" using sprintf() shows value 0.000 into " MyArray".

Or sometimes a random character like "/" appears in "MyArray".


I tried using snprintf as below but still same issue is observed.


snprintf(MyArray, sizeof(MyArray), "%5.3f", f_Value);


Please let me know if you have any solution to fix this problem.