[SOLVED] Problem with snprintf and signed and unsigned long

Hi
I want to use signed long and unsigned long with snprintf. after some reading up I think I should use the ul and il tag.
However that does not give me the wanted result.
For instance:

void setup() {
  // put your setup code here, to run once:
  delay(1000);

}

void loop() {
  // put your main code here, to run repeatedly:
  static unsigned long smallUnsigned =1;
  static unsigned long bigUnsigned =100000;
  static unsigned long smallSigned =-100000;
  static unsigned long bigSigned =100000;
  char printBuffer[100];
  snprintf(printBuffer,sizeof(printBuffer),"smallUnsigned =%ul",smallUnsigned++);
  Serial.println(printBuffer);
  snprintf(printBuffer,sizeof(printBuffer),"bigUnsigned =%ul",bigUnsigned++);
  Serial.println(printBuffer);
  snprintf(printBuffer,sizeof(printBuffer),"smallSigned =%il",smallSigned++);
  Serial.println(printBuffer);
  snprintf(printBuffer,sizeof(printBuffer),"bigSigned =%il\n",bigSigned++);
  Serial.println(printBuffer);
  delay (1000);

}

creates following output (that is from the start)

smallUnsigned =1l
bigUnsigned =34464l
smallSigned =31072l
bigSigned =-31072l

smallUnsigned =2l
bigUnsigned =34465l
smallSigned =31073l
bigSigned =-31071l

smallUnsigned =3l
bigUnsigned =34466l
smallSigned =31074l
bigSigned =-31070l

smallUnsigned =4l
bigUnsigned =34467l
smallSigned =31075l
bigSigned =-31069l

smallUnsigned =5l
bigUnsigned =34468l
smallSigned =31076l
bigSigned =-31068l

The last character is always a l which indicates it is not interpreted. As such all the longs are interpreted as ints and return wrong values as soon as the MSB is used.
Do I use the wrong tag or is this a bug?

Best regards
Jantje

Is that your real code? You don't call Serial.begin() so I wouldn't expect to see any output at all.

This sketch:

void setup() 
{
  Serial.begin(9600);

  char printBuffer[100];
  static unsigned long bigUnsigned = 0xFFFFFFFF;
  static long bigSsigned = -200000;
  snprintf(printBuffer, sizeof(printBuffer), "The unsigned output is '%lu'", bigUnsigned);
  Serial.println(printBuffer);
  snprintf(printBuffer, sizeof(printBuffer), "The signed output is '%ld'", bigSsigned);
  Serial.println(printBuffer);
}

void loop()
{
}

produces this output:

The unsigned output is '4294967295'
The signed output is '-200000'

Edit : changed the example to include large signed negative and unsigned positive values.

yes it is the real code.
Seems it works on the yun with minicom without the serial.begin();.
Anyway you are using lu and li instead of ul and li :D. Using those it works great :smiley:
So thank you very much
Best regards
Jantje