'sprintf leaves out double data C [closed]

The sprintf function is not including the variables which I have included. I have the following code:

    typedef struct
{
    double sensor_1;
    double sensor_2;
    double sensor_3;
    double sensor_4;
    int32_t acc_count;
}PAC1934_ResultData_t;

here is the struct for the power sensor.

PAC1934_ResultData_t power;
power = PAC1934_read_Power_accumulation();
uint8_t buf[100] = {0};
sprintf((char*)&buf, "  %010.9f W     %010.9f W     %010.9f W     %010.9f W    \n\r",
           power.sensor_1, power.sensor_2, power.sensor_3, power.sensor_4);

But the value of buf is always just some spaces and the W. I checked in the debugger and the values of power.sensor_1, ... are always correct and are non zero. The power values are all of the type double. The function PAC1934_read_Power_accumulation(); returns a element of type PAC1934_ResultData_t with values for all the elements of it. What is going wrong?

When I am rewriting the code with an integer, the sprintf works fine.

EDIT: Simple code just trying to output a float or a double doesn't work. Only integer output works. I have tested it with

double test = 1.432;
printf("%f",test);


Solution 1:[1]

Candidate problems:

No FP support @Tom V

Various compilers/linkers do not include floating point (FP) support automatically. Code with only FP via printf()/scanf() gets fooled into thinking no FP support needed. Research your compiler to force linking of FP support or maybe simple and some obvious FP code to your source code.

Buffer overrun

sprintf((char*)&buf, " %010.9f W ... may overrun. Is is good practice to be prepared for unexpected FP values and insure no overrun, not matter what. buf[100] with %010.9f is not enough.

Alternatives: @SparKot

// Limit buffer writing
snprintf((char*)&buf, sizeof buf, "  %010.9f W ...
// or 
// Use `%g` to avoid long text
sprintf((char*)&buf, "  %010.9g W ...   
// or 
// Increase buffer size to some worst case - think -DBL_MAX
uint8_t buf[4 * 350] = {0};

True failure? @M.M

OP only described a failure, but did not post the code that could replicate it.


General help

Enable all compiler warnings.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1