Peculiar behavior of sprintf()

(Using Firefox I couldn't see the New Topic button for Programming Questions forum only. I could see in other fora. For this reason I am using ?Edge? at the moment. )

#include <Wire.h>
#include "LiquidCrystal_I2C.h"

#define I2C_ADDR 0x3F  //Define I2C Address where the PCF8574A is ...  Nano: 0x27  Uno: 0x3F
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

//Initialise the serialLCD
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

typedef struct moving {unsigned long line_nr; char motor_name[10]; long nr_steps; unsigned int min_speed; unsigned int max_speed; float accel; float decel;};
#define NUMBER_OF_RECS 4
moving move_rec[NUMBER_OF_RECS]={
  {1, "M1", 10, 200000, 2000000, 0.97, 0.97},
  {2, "Stop", 0,0,0,0,0},

void setup() {
  lcd.begin(20, 4);

void loop() {
  char buffer[40];
  lcd.setCursor(0, 0);
  sprintf(buffer, "%s TEXT %dX", move_rec[0].motor_name, move_rec[0].line_nr);  // NOTE_1
  lcd.setCursor(0, 1);
  sprintf(buffer, "%d TEXT %sX", move_rec[0].line_nr, move_rec[0].motor_name);  // NOTE_2
  while (true){
    // stay forever

In line marked NOTE_! I get "M1 TEXT 1X"

In line marked NOTE_" I get "1 TEXT X" ... M1 does not show up.

I cant figure out why.


move_rec[0].line_nr is an unsigned long, but you are telling sprintf to print an int. You need to use %lu to print it as unsigned long. If the format does not match the data you pass, all following arguments will be mis-interpreted, as the sprintf will not parse the arguments correctly.

Thank you.

Indeed %lu works.

In aeronautics this is called tunnel vision. The more we look the less we see.


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