Unusual behaviour with printf

this code fragment

int8 gVerbose = 1;
printf("verbose: %d \r\n", gVerbose);

outputs “verbose: 1” as you would expect

this code fragment

int32 gVerbose = 1;
printf("verbose: %d \r\n", gVerbose);

outputs “verbose: 0” which surprised me
especially as the rest of the code thatuses gVerbose works fine!?!

(uint8 and uint32 behave the same as above)

Not sure if it is related but in example_usb_com.c there is a note about 32 bit numbers and sprintf (line 188).

good call
that would probably explain it
thanks!

note to developers - should the printf family support 32 bit integers?

Hello.

The printf function on the Wixel is provided by SDCC’s standard C library. The best documentation I could find for printf is here:

sdcc.sourceforge.net/doc/sdccman … 0000000000

According to that, there are different versions of printf available and not all of them support 32-bit integers. Unfortunately I don’t know how to tell SDCC which version of printf to use and I don’t know which one it is using by default (you could investigate this yourself). You might need to recompile C:\Program Files (x86)\SDCC\lib\medium\libsdcc.lib to get SDCC to use a version of printf that supports 32-bit integers.

–David

Hello,

I just figured out that SDCC’s printf does support 32-bit integers by default; to make it work, you just have to add an ‘l’ (lowercase L, for “long”) in front of the format specifier. For example, this code should work:

int32 gVerbose = 1;
printf("verbose: %ld \r\n", gVerbose);

- Kevin

1 Like