Trying to use dynamic formatting

hi, i understand you can use * to specify dynamic formatting, but whenever i try this i can't see anything in the Serial prompt, but if i use it hardcode, it works.

    size_t size=rtl ?_cols*2:_cols;
    char buffer[ size]="";
    va_list argptr;
    va_start(argptr, fmt);
    vsnprintf(buffer, sizeof(buffer), fmt, argptr);
    va_end(argptr);
    char buf[size]="";
    sprintf(buf,"%*s",20,buffer);
    //sprintf(buf,"%20s",buffer);
    Serial.println(buf);
    
    display(buf,0,row,rtl);

What exactly do you mean by this ?

Please post a complete sketch that can be compiled to demonstrate the problem and details of the board that you are using

dynamic formatting for sprintf means, that instead of specfying "%20d, the 20 would come from one of the variables in the call.
so in here, the * is actually being replaced by the 20
sprintf(buf,"%*s",20,buffer);

that's what i got from here:

As I asked

possibly thinking of an example such as

int main(void) {
char myNumber[50]={0};
double a=3.13419;
int A=15, B=2;
sprintf(myNumber,"%*.*lf",A,B,a);
printf("%s", myNumber);
}

gives a field width of 20 precision of 2

           3.13

I don't think the avr-libc version of printf() supports dynamic field widths.
I don't see them mentioned here: avr-libc: <stdio.h>: Standard IO facilities except as:

The variable width or precision field (an asterisk * symbol) is not realized and will to abort the output.

It also, ,at least on AVRS does not support the "f" specifier. And "lf" is precisely the same as "f" in any proper c/c++ implementation. All floats are, by default, promoted to double for printf arguments. And, of course, AVR Arduinos do not even support double - a double it exactly the same as a float.

thanks i was afraid of that, although actually i'm aiming for the %s rather than the float , guess i'm gonna hack it myself, although that doesn't work either..

 size_t size=rtl ?_cols*2:_cols;
    char buffer[ size]="";
    va_list argptr;
    va_start(argptr, fmt);
    vsnprintf(buffer, sizeof(buffer), fmt, argptr);
    va_end(argptr);

    
    char fmtbuf[3]="";    
    if (rtl){
        sprintf(fmtbuf,"%%%ds",size);        
    }else{
        sprintf(fmtbuf,"%%%ds",-size);        
    }

    char buf[size]="";
    sprintf(buf,fmtbuf,buffer);
    //sprintf(buf,fmtbuf,20,buffer);
    //sprintf(buf,"%20s",buffer);
    Serial.println(buffer);
    Serial.println(fmtbuf);
    Serial.println(buf);
    
    display(buf,0,row,rtl);

running

void setup() {
  Serial.begin(115200);
  Serial.println("sprintf test");
  char text[100] = { 0 }, test[] = { "test1" };
  sprintf(text, "%10s!", test);
  Serial.println(text);
  sprintf(text, "%*s!", 10, test);
  Serial.println(text);
  sprintf(text, "%*s!", 5, test);
  Serial.println(text);
  Serial.println("test done!");
}

void loop() {}

on a UNO the dynamic formatting fails

sprintf test
     test1!


test done!

on an ESP32 works OK

sprintf test
     test1!
     test1!
test1!
test done!

Doesn't "%20s" take up more than 3 characters?

< deleted > I see this had already been mentioned.

consider

sprintf test
     test1!
test2!
               test3!
test done!
void setup() {
    Serial.begin(115200);

    Serial.println("sprintf test");
    char text [80];
    char fmt  [10];

    sprintf(text, "%10s!", "test1");
    Serial.println(text);

    sprintf(fmt, "%%%ds!", 5);
    sprintf(text, fmt, "test2");
    Serial.println(text);

    sprintf(fmt, "%%%ds!", 20);
    sprintf(text, fmt, "test3");
    Serial.println(text);

    Serial.println("test done!");

}

void loop() {}

As mentioned by @westfw, sprintf for Arduino has been emasculated for space reasons. I do recall though that there is a way to get %f working - presumably by replacing the library responsible. It might be worth a little research to see if it actually gives you the full monty.

thanks @johnwasser and everyone, i'll try those, what i'm currently trying to do and failing is using clang-tidy in platform.io which should at least be able to give me SOME warnings like the ones about the length of buffer and sprintf, but it fails becuase it also gives me errors on the avr system libraries which i can't get rid off.
what's some stranger, is that i've noticed that at some points the LCD flickers very fast and not sure why.. i have don't place any delays in my loop when i update the LCD

can't help without seeing code and errors

are you only updating the LCD when the output changes?

not exctly, the first line is updated all the time, during development i also display the seconds, which is why i refresh it
the other 3 lines are displayed only the date changes (or when i press button to change the date)

btw as you can see the lines can change very dynamically and i can' clear them i get remains of letters.

i'm working on uploading the code on github, but i was planning to re-regonize it first a little bit .


hard to say what the problem might be without seeing the code

publish my code initiallly
this is my first CPP project, i'm a java developer

it starts here:

uses HClock/ClockController.h at master · emaayan/HClock · GitHub (yes i know, code needs to be in cpp file, i'll get to it, it's harder when platformio barley has refactor features
and HClock/DisplayWrapper.cpp at master · emaayan/HClock · GitHub
which displays things

looks like quite a bit of code i'm not enthusiastic about digging thru, especially without clearly understanding the problem. the display wrapper around the lcd functions seems unnecessary

i thought the characters looked like hebrew but initially thought they were corrupted. now i see it's for a Jewish clock

not clear what the problem is. guessing you may be partially updating with a line shorter than the one previously displayed.

if so, you could clear and completely update all lines on the display, or if you want to partially update a line using col/row, make sure you append spaces to the c-string being passed so that the # of cols is the same each time

that's why i couldn't exactly post the code :), the LCDWrapper was meant to absract the LCD api's incase i might change them, (for example switching to an OLED display)
i'll try to re-create it. not sure how far i'll go with the clang-tidy