Sprintf array assignment value lost

Hi,
I receive from my RTC day, month and year.
I want to format those throughout my program in DD-MM-YYYY (so January should be "01" and not "1").

I thought of using sprintf, but I came to the conclusion that - even though the value gets assigned to a buffer - you lose that value immediately after another sprintf is used...

How to get the same result and allow the dateDDMMYYYY to be used throughout my program?

Global assignment:

char dateDDMMYYYY[10];
char timeHHMM[5];
char timeHHMMSS[8];
void updateTime() {
  lcd.setCursor(0, 0);

  //printf consumes more memory. Can also try even snprintf_P */
  sprintf(dateDDMMYYYY, "%02d-%02d-%04d", RTC.getDay(), RTC.getMonth(), RTC.getYear());
  //Serial.println(dateDDMMYYYY); --> Here it prints!
  sprintf(timeHHMM, "%02d:%02d", RTC.getHours(), RTC.getMinutes());                           //printf consumes more memory. Can also try even snprintf_P */
  sprintf(timeHHMMSS, "%02d:%02d:%02d", RTC.getHours(), RTC.getMinutes(), RTC.getSeconds());  //printf consumes more memory. Can also try even snprintf_P */
  Serial.println(dateDDMMYYYY); // --> Here it doesn't!
  lcd.print(dateDDMMYYYY);
  lcd.print("-");
  lcd.print(timeHHMM);
}

you overflow ➜ you need 11 bytes in dateDDMMYYYY - 10 for the DD-MM-YYYY content and then one for the trailing null char so

char dateDDMMYYYY[10];

is not good.

the easiest way if you don't want to count (but costs a but of memory) is to let the compiler do the work for you

char dateDDMMYYYY[] = "DD-MM-YYYY";

otherwise go for

char dateDDMMYYYY[11];

also, it's better to use snprintf() this way you won't overflow

check the other buffers too

Thank yo so much J-M-L!
I really didn't know about the extra character required at the end (and never included that).
Fixed all the buffers, and works like a charm now!
Thanks!!
(since I know the size for each, I went for the least memory consuming option).

good

and to be on the safe size go for snprintf() (see https://cplusplus.com/reference/cstdio/snprintf/)

  snprintf(dateDDMMYYYY, sizeof dateDDMMYYYY, "%02d-%02d-%04d", RTC.getDay(), RTC.getMonth(), RTC.getYear());

(EDIT: corrected to add the missing n - thanks @Delta_G )

have fun

I effectively tried that after your first remark, but got issues when reading (after writing) again from the SD card, while with regular printf it seemed to go fine.

  sprintf(dateDDMMYYYY, sizeof dateDDMMYYYY,"%02d-%02d-%04d", RTC.getDay(), RTC.getMonth(), RTC.getYear());
  sprintf(timeHHMM,sizeof timeHHMM, "%02d:%02d", RTC.getHours(), RTC.getMinutes());                           
  sprintf(timeHHMMSS,sizeof timeHHMMSS, "%02d:%02d:%02d", RTC.getHours(), RTC.getMinutes(), RTC.getSeconds());  
  lcd.print(dateDDMMYYYY);
  lcd.print(" ");
  lcd.print(timeHHMM);

The last lines below are when using sprintf, while the two lines above were related to printf.

printf_sprintf

Same thing on the LCD display.
Ideas? :slight_smile:

if you want to pass the buffer size, use snprintf() (notice the n in the function name)

2 Likes
DateTime dateTime(F(__DATE__), F(__TIME__));
char buffer[50] {"DDD, YYYY.MMM.DD hh:mm:ss"};
Serial.println(dateTime.toString(buffer));

:face_with_head_bandage:

Damned - thanks for that... indeed... (corrected)

--
of course I would never do a copy/paste mistake like that, it was all intentional to see if @maartenvd84 was paying attention... :innocent:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.