Difference in serial output chars for AVR vs ESP - where to start?

So, the same source code gives different output on the serial terminal depending on the target board being an ESP32, vs an AVR Mega328P. I have tried a few variants of each, so it's not a board phenomenon within each family - what can I start looking at?

The code is below, it should (and does, on ESP32) print the compiled date and time as a string to the terminal. I get different output for AVR compare to ESP32:

ESP32 output example: "2025-05-17-18-54-01"
AVR output example: "2025-05-17-..." <-- the TIME part changes, often including non printable chars or high ASCII values that print as symbols.

char versionString[] = "YYYY-MM-DD-HH-MM-SS";   // declared like this to tell the compiler it's size, and make obvious to user on serial print if it has not been set

void dateToVersionString(char *buff) {  
    int month, day, year;
    char date[] = __DATE__;
    String time = __TIME__;
    static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
    sscanf(date, "%s %d %d", buff, &day, &year);
    month = (strstr(month_names, buff)-month_names)/3+1;
    time.replace(":","-");  
    sprintf(buff, "%d-%02d-%02d-%s", year, month, day, time); 
}


void setup()
{
  Serial.begin(115200);
  dateToVersionString(versionString);
}

void loop() {
  delay(1000); 
  Serial.println(versionString);   
}

Try this instead

    sprintf(buff, "%d-%02d-%02d-%s", year, month, day, time.c_str()); 

printf's %s expects a plain C-string, a char *

  • with ESP32 (and others) String employs SSO: Small String Optimization. The object is larger, and for short text -- 14? 13? characters or fewer -- the string is stored at the start of the object, avoiding a heap allocation. This can "waste" memory, but avoids fragmentation.
  • with AVR, the object is smaller, but the actual text is always allocated on the heap. No waste, but more fragmentation.

String::c_str returns the pointer to the text, wherever it is

I suggest that you set Compiler warnings to All under file/preferences. If you already did that, don't ignore the warning :wink:

The relevant part in result of compiling your sketch:

C:\Users\bugge\AppData\Local\Temp\arduino_modified_sketch_736568\sketch_may18a.ino: In function 'void dateToVersionString(char*)':
C:\Users\bugge\AppData\Local\Temp\arduino_modified_sketch_736568\sketch_may18a.ino:11:58: warning: format '%s' expects argument of type 'char*', but argument 6 has type 'String*' [-Wformat=]
   sprintf(buff, "%d-%02d-%02d-%s", year, month, day, time);
                                                          ^

Tested, and of course working with your suggested change.

Thank you for taking the time - as is often the case, once you know the direction to look, it's easy :slight_smile: I have a number of tabs open to read on string optimisation now...

Great suggestion - I have increased the warning verbosity. Hard not to feel like your avatar pic when the compiler was able to tell me the issue all along :expressionless: