sprintf and floats or unsigned longs

sprintf is not doing what I expect

#include <stdio.h>

unsigned long Encoder_pos = 0;

void setup() 
{
      char message[20];
      Serial.begin(57600);      // for debug

      sprintf(message,"%u",Encoder_pos - 1);
      Serial.println (message);
        Serial.println (Encoder_pos - 1);
}

void loop() 
{
}

results:

65535
4294967295

Add a lower case L to the format string...

      sprintf(message,"%lu",Encoder_pos - 1);

Thank you CB!
I have not seen that before. I ref. this page and it does not mention it:
http://www.space.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/format.html

Where did you find it?

Keshka

I hate to say it but I've been writing code long enough that I no longer remember.

I typically use the MSDN (via Google) to lookup C(++) things...

It's mentioned in "K-R", the book "The C Programming Language" by Brian Kernighan and Dennis Ritchie.

There's also a good summary in Wikipedia: printf - Wikipedia.

-Mike

Someone suggests to use snprintf() instead of sprintf() for somewhat code safety problem.I am a newer, not knowing why but that snprintf() is a similar thing to sprintf(), and they have the same form to use.

Using snprintf is good advice and it appears to be available for the Arduino.

sprintf has a heavy footprint (almost 2k of code) and requires a RAM buffer. For simple printing using multiple Serial.print statements uses much less code and no RAM buffer. For more complex printing, have a look at Mikal Hart's print streaming library: New Streaming Library | Arduiniana

This does not need a RAM buffer and uses no more code than the equivalent Serial.print functions.