While working on an embedded project, I noticed that sprintf()
method for the following code:
char ln2[16];
sprintf(ln2, "%f Volt", Data[Position].Voltage1);
generates the question mark character as output.
The output generated from the code above is:
? Volt
while the input is declared as double Voltage1 = 0.0;
The same issue does not seem to apply while trying to format an integer.
The following works as expected:
char ln1[16];
sprintf(ln1, "POSITION %d", (Position + 1));
and outputs POSITION 3
where the Position
is a global variable and declared as int
.
The structure that actually holds the data is:
struct data
{
int Position;
double Voltage1;
double Voltage2;
};
All above while using the C/C++ and the Platform.io extensions both for VS Code.
What is going on wrong here?
Embedded versions of the printf usually do not implement float number handling. You need to let the linker to link the correct version of the function. For example for ARM gcc it will be -u _printf_float
or/and -u _scanf_float