#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.print("sprintf example");
  delay(2000);
  char buffer [50];
  int n, a = 7, b = 3;
  n = sprintf (buffer, "%d times %d is %d", a, b, a * b);
  Serial.println(buffer);
  lcd.setCursor(0, 1);
  lcd.print(buffer);
  delay(2000);
  Serial.println("but");
  //lcd.setCursor(0, 1);          // this doesn't clear the previous chars
  //lcd.print("but");             
  delay(2000);
  lcd.setCursor(0, 1);            // this does clear the previous chars
  lcd.print("but             ");    
  delay(2000);
  b = 4;
  n = sprintf (buffer, "%d times %d is %d", a, b, a * b);
  Serial.println(buffer);
  lcd.setCursor(0, 1);
  lcd.print(buffer);
}

void loop ()
{

}