Using printf in an Arduino Uno

Hi,

I'm using the printf() wrapper, as per the second option in this article (Arduino Playground - HomePage)

I copied the code:

#include <stdarg.h> // for the printf wrapper

//printf wrapper
void p(char *fmt, ...){
char buf[128]; // resulting string limited to 128 chars
va_list args;
va_start(args, fmt);
vsnprintf(buf, 128, fmt, args);
va_end(args);
Serial.print(buf);
}

void p(const __FlashStringHelper *fmt, ...){
char buf[128]; // resulting string limited to 128 chars
va_list args;
va_start(args, fmt);

#ifdef AVR
vsnprintf_P(buf, sizeof(buf), (const char *)fmt, args); // progmem for AVR
#else
vsnprintf(buf, sizeof(buf), (const char *)fmt, args); // for the rest of the world
#endif
va_end(args);
Serial.print(buf);
}

I have a function, getNumberOfClicks(), that returns an uint8_t.

I run this code:

p("Got into PRESSED. t = %ums, ", millis());
p("n = %u\n", machine.getNumberOfClicks());
p("Got into PRESSED. t = %ums, n = %u\n", millis(), machine.getNumberOfClicks());

And I get the following output:

Got into PRESSED. t = 28514ms, n = 1
Got into PRESSED. t = 28517ms, n = 0

The first line is correct, n IS 1, I don't get why, when I run p with two arguments, I get 0! Can somebody point me to my error???

I tried the third method too, "Hooking up printf() on AVR", and I get this output:

Got into PRESSED. t = 8141ms,n = 1
Got into PRESSED. t = 8144ms, n = 6

Again, only the first option is correct, n IS equal to 1, I don't get where the 6 comes from.

I'm using v1.8.4 of the IDE and 1.6.20 of the boards. I am really puzzled by this!

You are calling get number of clicks twice in row. Does the click count really changes between these calls?