Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sprintf format issue

Hi,

I am doing a simple thing with string formatting:

sprintf(tmp_str,"%d.%d.%3d", version.major, version.minor, version.patch);

The version structure has values { 0, 5, 1 } and char tmp_str[20]. I want to achieve string like "0.5.001". Instead of getting this format, I am getting "0.5.d". 

For a curiosity I tried to change the format to "%3d.%d.%d" and I got "d.0.5". Format "%d.%d.%d" works as expected - 0.5.1.

I am using SDK 17.0.2 and SES 5.60.

Can somebody advise me how to brush sprintf to work as expected? I am not asking for workarounds how to get the expected string format rather to get sprintf working.

Thanx.

Best regards,

peter

Parents Reply Children
  • Hi Peter

    Then I am running low on ideas, it works fine here ;)

    Are you able to zip your project folder (including project and source files) and share it with me, so I can test out your exact configuration?

    You might want to remove the build files first, to reduce the file size. 

    Best regards
    Torbjørn

  • Hello Peter and Torbjørn,

    Peter I realize a few days have passed since your initial post.  Have you been able to solve your printf() formatting issue yet?  Today while working on a different project and problem I came across a new project dialog in Nordic's version of Segger (Nordic SES release 5.6), with extra support for nRF Connect SDK projects.  A "Common Project Settings" dialog there catches my eye.  There are references to several printf functionalities, most of which are disabled by default.

    One of the options is called "printf width/precision".  Here is a window capture of this dialog box:SES "Choose Common Project Settings"

    When I initially wrote and suggested to try "%03d" format specifier in place of "%3d" specifier, I first wrote a short C program to be sure this specifier worked.  Running the program on the host workstation confirmed this.  The version of C which I was able to test against is:

    $ /lib/x86_64-linux-gnu/libc.so.6
    GNU C Library (Ubuntu GLIBC 2.31-0ubuntu9.2) stable release version 2.31.

    Compiling C natively under Ubuntu 20.04, libc6 printf libraries are fully enabled by default.  Printf family functions however, as you likely know, can be too large for microcontrollers with limited flash and other memory, so significant parts (or all) of printf formatting are often disabled by default in firmware development settings.

    Perhaps in your project environment some of these printf functionalities are disabled right now, like the ones in the window capture above?

    - Ted

  • Hi Ted,

    thanx for your suggestion. We moved a bit, still not at the goal.

    I had the printf precision off so I changed it to yes. The result is as follows:

    - when using string format "%d.%d.%3d" the output is "v0.5.  1"

    - when using string format "%d.%d%.3d", the output is "v0.5001"

    A bit better but still not what we would like to see :)

    Best regards,

    peter

  • Hi Ted,

    I realized I need to use the following string format: "%d.%d.%03d". Then I am getting the output as expected: "0.5.001".

    Thanx a lot, Ted and Torbjørn, for an excellent support.

    Best regards,

    peter

  • Hi  Peter

    I assume "%d.%d.%.3d" would also work? 

    Anyway, excellent to hear that you were able to get it working Slight smile

    Best regards
    Torbjørn

Related