bug in printf() formatting of 64-bit types

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
cogoc_1937206
Level 4
Level 4
First like received

printf() doesn't seem to handle the format specifiers for 64-bit types correctly.  It appears as though it retrieves 4-bytes of junk from the stack and formats that in place of the actual variable.  Interestingly, attempting to print two 64-bit variables yields the correct value for the low-order 4 bytes of the first variable in place of the second format specifier.  In this case, the second variable being printed gets missed altogether.

Some example code:

unsigned long long int bigint = 0x00000005FFFFFFFF;

printf("bigint: %llu\r\n", bigint);

printf("bigint: %llu, %llu\r\n", bigint, bigint);

printf("bigint-32: %lu\r\n", (unsigned long int)bigint);

printf("bigint-hex: %#llx, %#llx\r\n", bigint, bigint);

printf("bigint-hex32: %#lx\r\n", (uint32_t)bigint);

Which yields the following:

bigint: 1

bigint: 1, 4294967295

bigint-32: 4294967295

bigint-hex: 0x1, 0xffffffff

bigint-hex32: 0xffffffff

Where one would expect:

bigint: 25769803775

bigint: 25769803775, 25769803775

bigint-32: 4294967295

bigint-hex: 0x5ffffffff, 0x5ffffffff

bigint-hex32: 0xffffffff

I should note that I'm using WICED v2.4.1.

Cheers!

4 Replies
GregG_16
Employee
Employee
50 sign-ins 25 sign-ins 25 comments on KBA

Hello,

I have given you access to SDK3.0.x, can you verify it is still a issue?

0 Likes

Hi,

We're using Inventek's dev kits, so I've asked them if they have a patch for 3.0.X to add support for their platform.  (Similar to what they have for 2.4.X.)  Will get things running and try printf()s with 64-bit types once I get that from them.

Cheers!

0 Likes

Hi,

Wiced SDK does not support long long printf().

We do not have plans to support it in near future.

For workaround perhaps the following could be used:

printf("bigint-64: 0x%lx%lx\r\n", (unsigned long int)(bigint>>32), (unsigned long int)bigint);

Seyhan

0 Likes
Anonymous
Not applicable

Actually, it is not an issue with the SDK, it is related to the runtime library coming from ARM. printf is a standard LIB function.

I would think long long on printf can (should) work: you have to use the correct format specifier, e.g. "%lld". A simple "%x" or even "%lx" cannot work: "lx" is long hex, but long is 4 byte.
long long as 8 bytes needs carful use of related type specifier, assuming "llx". The format specifier tells the code how to take the parameters, as 4 or 8 bytes (from registers or stack). A mismatch results in displaying garbage and all following parameters printed wrong.
But it depends really if the common ARM runtime LIB does support (the printf implementation, actually vsprintf, also for sprintf used etc.).

If long long works in general, e.g. to define as variable, to do some math with it - actually printf should work as well, with "%lld"  (two L's !).

Please, see here:

ARM Information Center

0 Likes