This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C129XNCZAD: sprintf problem with parameters

Part Number: TM4C129XNCZAD

Tool/software: Code Composer Studio

hello everyone, i have a problem when i pass option to  the sprintf.

for example, if i want to print a floating point value with 6 charaters i will do

sprintf(mystring, "%06f", myfloat);

if i have myfloat = 0 the result will be mystring = "0.000000" an 8 character string.

  • Hello Davide,

    Float printing with functions like sprintf aren't always supported by the library used to include the sprintf function. You would first need to see if your sprintf function even supports what you are attempting to do.

    The topic of printing floats came up last week and I addressed it for one user in this post with a different method, you may find it helpful: https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/p/663530/2438264#2438264

    Also in another post, the user solved an issue with sprintf (which did support floating points) by increasing his stack size: https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/199796

  • In addition to Ralph's very good suggestions:

    1. If available with your compiler toolchain, consider using snprintf() rather than sprintf(). With snprintf() you must provide the size of the buffer. Because it "knows" the size of the buffer, snprintf() function will not overrun the buffer and the resulting string will always be null-terminated. In the worst case scenario, the resulting string is truncated to fit within the buffer, and you can detect this condition by checking the return value of snprintf(). With the older sprintf() which does not "know" the buffer size, there is the possibility of overrunning that buffer and corrupting other memory in use by your program. The snprintf() function is available with most reasonably modern compilers.

    2. I am not sure what output string you expected, but the format string you want is probably "%0.6f" (notice the decimal point). This will format your result to 6 decimal places.

    Example using snprintf():

    char mystring[20];
    
    float myfloat = 1.2f;
    
    snprintf(mystring, sizeof(mystring), "%0.6f", myfloat);
    

    mystring should now contain "1.200000"

    Note that if you pass a string buffer to one of your own functions which then passes it to snprintf(), you will also need to pass the size to your own function:

    void myfunction(char * resultString, size_t sizeOfResultString, float floatValue)
    
    {
    
    snprintf(resultString, sizeOfResultString, "%0.6f", floatValue);
    
    }
    

    Do not make this mistake:

    void myfunction(char * resultString, float floatValue)
    
    {
    
    snprintf(resultString, sizeof(resultString), "%0.6f", floatValue); // <--- WRONG!!
    
    }

    (The mistake here is that you would pass the size of the pointer to resultString, not the size of the buffer pointed-to by resultString.)

  • Thanks for your message, in this day i'm not working on this project but i will let you know ASAP.

  • thanks for your message, answering to your point, i have already tryed the snprintf function but i get the same result, so i used it to truncate the conversion in this way

    char mystring[6 + 1]; // + 1 for the terminating ascii null character
    
    float myfloat = 123.456789;
    
    snprintf(mystring, sizeof(mystring), "%06f", myfloat);

    so the string will be actually truncated at 6 character, so mystring will contain "123.45\0" because i want a 6 characters length string and not 6 decimal.

    i have also tryed to use a pointer char variable and get the dimension with the malloc but i don't know if it's possible with the microcontroller, i usually develop windows software and not micro software.

  • Hello Davide,

    Davide Vittorio Gozzini said:
    i have also tryed to use a pointer char variable and get the dimension with the malloc but i don't know if it's possible with the microcontroller

    Coming from the other end of the spectrum (less knowledge about Windows software), I am not sure I fully can grasp what you are attempting but in general as long as the type is defined (chat, int, float, uint8_t, etc.) that portion with the pointer should be fine, and malloc is available provided the MCU is programmed to reserve some RAM as 'heap' memory for such operations. I am not sure what 'get the dimensions' means though so can't comment on that based on my current level of understanding.

  • The printf() family of functions will not truncate a number the way you're hoping to do. When there is a conflict between formatting (e.g., you want the resulting string to be 6 characters long) and accuracy (e.g., you try to print 1000000 which is 7 characters long), the printf() functions will favor accuracy at the expense of formatting and print the entire number. That is because truncating the number will not give the correct answer. If you try to print "1000000" and it gets truncated to "100000" that is not correct. (If someone owes you $1,000,000 and they try to get away with paying only $100,000, you will not be happy.)

    If you know what range your numbers can have, e.g., if you know the numbers will always be from 0 to 999, you can try "%.2f". This will leave enough room for three digits for 0 through 999, plus a decimal point, plus two decimal digits, for six characters total (requiring a buffer of at least 7 characters for the trailing NUL). So it will output anything from "0.00" to "999.99" however note that if you give it a larger number such as 1000 or larger, or a negative number, the result will consequently take up more than six characters, e.g., "1000.00" is 7 characters; "-999.99" is also 7 characters; "-1000.00" is 8 characters! Of course in every case there is always the trailing NUL as well so the buffer must be at least 9 characters.
  • i actually know that my value will be from -10.00 to 10.000 so a 6 character + terminating \0 so i'm going only to truncate the last decimal.

    now i have this problem "mkhex4bin failure occurred.  Giving up."

  • i add this information, the same software on windows 7 with CCS 6.2.0.00050 works correctly.
    the problem is on windows 10 with CCS 7.4.0.00015.
    both use compiler TI v5.2.6
  • as described here

    In reply to Warren Woolsey:
    
    I ended up doing a full uninstall and reinstall of CCS, this seems to have fixed the issue.
    
    CCS v6.1.1.00022
    
    Hercules TMS570LC43xx & MSP430F2619
    
    Tv 5.2.6

    also for me worked a reinstall and a reboot.

    for the sprintf i have resolved a problem with the stack size, the other truncating with the snprintf like described.

    from this thread i have learned that with the micro, different from windows, i cannot use too large array for my variable.

    i think for now it's all and the thread can be closed.

    thank you all for your support