OK, I created a sketch to keep this to the minimum to demo the warning, Line 31 seems to work OK with the:
sprintf(Text_tmp, "%02lu", tmp_x); // Line 31
But adding the %s throws warnings? (Lines 22 and 39) - but that's a different warning than the "int" vs "long unsigned int".
unsigned long tmp_x = 123456;
char Text_tmp [120] = "HOURS: ";
char Text_tmp_2[120] = "";
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
tmp_x = ( ((tmp_x > 99) ? 99 : tmp_x) );
sprintf(Text_tmp, "%s%02d", Text_tmp, tmp_x); // Line 14
// compiler gives warning on above, tmp_x int32 into a %2d
// but since I limit it to 99 value max, it is OK to ignore warning.
Serial.println(Text_tmp);
// Try this:
sprintf(Text_tmp, "%s%02lu", Text_tmp, tmp_x); // Line 22
// also get compiler warnings
Serial.println(Text_tmp);
// Try this - without the %s prefix:
sprintf(Text_tmp, "%02lu", tmp_x); // Line 31
// no warning w.o the %s???
Serial.println(Text_tmp);
// Try a different char string in source/dest????
sprintf(Text_tmp, "%s%02lu", Text_tmp_2, tmp_x); // Line 39
Serial.println(Text_tmp);
}
I added LIne #'s in the comment to help match up with the compiler warnings:
...sketch_apr20a.ino: In function 'void loop()':
...sketch_apr20a.ino:14:21: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
14 | sprintf(Text_tmp, "%s%02d", Text_tmp, tmp_x); // Line 14
| ^~~~~~~~ ~~~~~
| |
| long unsigned int
...sketch_apr20a.ino:14:21: warning: '%02d' directive writing 2 bytes into a region of size between 1 and 120 [-Wformat-overflow=]
14 | sprintf(Text_tmp, "%s%02d", Text_tmp, tmp_x); // Line 14
| ^~~~~~~~
...sketch_apr20a.ino:14:21: note: directive argument in the range [0, 99]
...sketch_apr20a.ino:14:10: note: 'sprintf' output between 3 and 122 bytes into a destination of size 120
14 | sprintf(Text_tmp, "%s%02d", Text_tmp, tmp_x); // Line 14
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...sketch_apr20a.ino:14:10: warning: 'sprintf' argument 3 overlaps destination object 'Text_tmp' [-Wrestrict]
...sketch_apr20a.ino:2:6: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
2 | char Text_tmp [120] = "HOURS: ";
| ^~~~~~~~
...sketch_apr20a.ino:22:21: warning: '%02lu' directive writing between 2 and 10 bytes into a region of size between 1 and 120 [-Wformat-overflow=]
22 | sprintf(Text_tmp, "%s%02lu", Text_tmp, tmp_x); // Line 22
| ^~~~~~~~~
...sketch_apr20a.ino:22:10: note: 'sprintf' output between 3 and 130 bytes into a destination of size 120
22 | sprintf(Text_tmp, "%s%02lu", Text_tmp, tmp_x); // Line 22
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...sketch_apr20a.ino:22:10: warning: 'sprintf' argument 3 overlaps destination object 'Text_tmp' [-Wrestrict]
...sketch_apr20a.ino:2:6: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
2 | char Text_tmp [120] = "HOURS: ";
| ^~~~~~~~
...sketch_apr20a.ino:39:22: warning: '%02lu' directive writing between 2 and 10 bytes into a region of size between 1 and 120 [-Wformat-overflow=]
39 | sprintf(Text_tmp, "%s%02lu", Text_tmp_2, tmp_x); // Line 39
| ^~~~~~~~~
...sketch_apr20a.ino:39:11: note: 'sprintf' output between 3 and 130 bytes into a destination of size 120
39 | sprintf(Text_tmp, "%s%02lu", Text_tmp_2, tmp_x); // Line 39
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~