I was under the impression that my outcome would be:
Temperature: 111
But it' not. The result is 16.
The msg[] should be the width of the message (per example), the %03d is a width of 3 and trailing 0's if the width is less than 3. The 'd' is integer value of setting2.
I don't know where the 16 comes from (it's not the value of msg[]).
sprintf return value: If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure.
I have a constrain on setting2 so it should never be larger than 3 digits. As for the '\O'... Do I need to reserve a position for it or is it something I should add?
The reference says:
Some programs imprudently rely on code such as the following sprintf(buf, "%s some further text", buf);
Can you please show an example of how to use the \O?
As for the '\O'... Do I need to reserve a position for it
Absolutely.
or is it something I should add?
Any string function will automatically add the NULL terminator. If you are directly adding to a string (char array), you should put the NULL terminator in the next position in the array whenever you add a character.
Can you please show an example of how to use the \O?
Sure, if you explain how you want to use the NULL.
sprintf() is a very powerful function, but most programs only use a very small portion of that power. In the code below, commenting out the str*() functions and using just sprintf() yields a flash size of 3302 bytes: