This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sprintf with a uint64_t variable?

Is there a compiler/makefile flag that allows for 64bit printing?

I have included inttypes.h, and tried both %llu and "PRI64" as shown:

    char scrbuf[30];
    uint64_t time_alive = 12345678;
    uint8_t tempret =0;

	sprintf(scrbuf," "%llu  "",time_alive );
	tempret = sprintf(scrbuf, ""PRIu64"    ",time_alive );

In both cases, the only thing that gets pushed to 'scrbuf' are the values of "l" and "u" to the first and second memory positions. I step through and view each of them in the debugger.

Any insight as to what I may be missing? Some sort of #define or some compiler flag? I am using the BLE HRS example as a base. This is all nrf51_SDK_10.0.0

Parents
  • Resolved.

    My specific problem stemmed from the fact that I was running a super stripped down version of newlib library and didn't even know it.

    In my makefile, I had the line

      LDFLAGS += --specs=nano.specs -lc -lnosys
    

    which seems to be the bare-essentials to run things like sprintf, etc. You disable it by putting a pound sign in front of the statement (for those who dont know, the '#' is how you comment out a line in a makefile)

      #LDFLAGS += --specs=nano.specs -lc -lnosys   <-COMMENTED OUT
    

    For folks that are new, look/research for libc or newlib and its associated documentation. Some scripting games are played with the nano directive to reduce the newlib to the absolute essentials to run. It stripped out the ability for sprintf to handle 64bit values, which held me up in my project...

    Hope that helps.

  • Note that not using nanolib will increase your compiled binary size by ~20k.

Reply Children
No Data
Related