Using floats with sprintf() in embedded C

后端 未结 13 1777
别跟我提以往
别跟我提以往 2020-12-24 06:35

Guys, I want to know if float variables can be used in sprintf() function.

Like, if we write:

sprintf(str,\"adc_read = %d \         


        
相关标签:
13条回答
  • 2020-12-24 06:57

    use the %f modifier:

    sprintf (str, "adc_read = %f\n", adc_read);
    

    For instance:

    #include <stdio.h>
    
    int main (void) 
    {
        float x = 2.5;
        char y[200];
    
        sprintf(y, "x = %f\n", x);
        printf(y);
        return 0;
    }
    

    Yields this:

    x = 2.500000

    0 讨论(0)
  • 2020-12-24 06:58

    Similar to paxdiablo above. This code, inserted in a wider app, works fine with STM32 NUCLEO-F446RE.

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    void IntegFract(char *pcIntegStr, char *pcFractStr, double dbValue, int iPrecis);
    
    main()
    {
       char acIntegStr[9], acFractStr[9], char counter_buff[30];
       double seconds_passed = 123.0567;
       IntegFract(acIntegStr, acFractStr, seconds_passed, 3);
       sprintf(counter_buff, "Time: %s.%s Sec", acIntegStr, acFractStr);
    }
    
    void IntegFract(char *pcIntegStr, char *pcFractStr, double dbValue, int 
    iPrecis)
    {
        int iIntegValue = dbValue;
        int iFractValue = (dbValue - iIntegValue) * pow(10, iPrecis);
        itoa(iIntegValue, pcIntegStr, 10);
        itoa(iFractValue, pcFractStr, 10);
        size_t length = strlen(pcFractStr);
        char acTemp[9] = "";
        while (length < iPrecis)
        {
            strcat(acTemp, "0");
            length++;
        }
        strcat(acTemp, pcFractStr);
        strcpy(pcFractStr, acTemp);
    }
    

    counter_buff would contain 123.056 .

    0 讨论(0)
  • 2020-12-24 06:59

    Isn't something like this really easier:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char str[10];
    float adc_read = 678.0123;
    
    dtostrf( adc_read, 3, 4, temp );
    sprintf(str,"adc_read = %10s \n", temp);
    printf(temp);
    
    0 讨论(0)
  • 2020-12-24 07:01

    Yes, use %f

    0 讨论(0)
  • 2020-12-24 07:02

    Don't do this; integers in C/C++ are always rounded down so there is no need to use the floor function.

    char str[100]; 
    int d1 = value;
    

    Better to use

    int d1 = (int)(floor(value));
    

    Then you won't get rounding up of the integer part (68.9999999999999999 becomes 69.00..). 68.09999847 instead of 68.1 is difficult to avoid - any floating point format has limited precision.

    0 讨论(0)
  • 2020-12-24 07:04

    Yes, and no. Despite what some other replies have said, the C compiler is required to perform conversions for sprintf(), and all other variadic functions, as follows:

    • char => int
    • short => int
    • float => double

    (and signed/unsigned variants of the above integral types)

    It does this precisely because sprintf() (and the other print()-family functions) would be unusable without it. (Of course, they're pretty unusable as it is.)

    But you cannot assume any other conversions, and your code will have undefined behaviour - read: crash! - if you do it.

    0 讨论(0)
提交回复
热议问题