Sprinf has unexpected output?

I added the following to the RSRX62N2201DEMO.c sample program on the RDK:

#include <string.h>

// global variables //

int current_headline = 1;
unsigned char headline_name[15];

// inside void main( void )
int val = 0;

while (1) {

val = sprintf (headline_name, "Head%d.wav", current_headline);

fill_wav_buf();  //function uses TFAT library to read the Head1.wav file and save data to the buffer

}

The first time the sprintf statement executes, everything is fine and the headline_name becomes "Head1.wav". Then just before the second execution, headline_name is "Head1.wav" as expected from a global variable. Then after execution of the sprintf, the headline_name variable becomes "Head           ". The variable val becomes 514 instead of the 9 when it is working properly.

Can anyone tell me what's going on here?

  • Which toolchain are you using?  Which libraries?

  • I am using the standard library string.h that is included with the Renesas Toolchain installation.

  • Mike,

    I had a few weird experiences with Sprintf too.

    (Using HEW)

    What I found:

    1) I didn't have the header file included in the C module.  Everything compiled and linked but the code worked strangely sometimes.

    2) When my string array was local to a C module I also had problems.

    What I did:

    By including the headers

    #include
    #include

    And using static local variables (or globals) for my string arrays, my problems went away.

    I specified the size of the variable to be printed (as %3d instead of %d)

    I also terminated the string with either \0 or \r\n depending on where I expected to use the string again.

  • Thanks for all your suggestions. I will try your recommendations and let you know what I find out. I kind of think the problem has to do with the string not be terminated properly. When I walk the function through the assembly code, I see it write 'H 'e' 'a' 'd' then tons of spaces. It appears to get messed up right at the %d.

    Another thing I thought about trying is:

    val = sprintf (headline_name, "Head%dwav\0", headline_num);

    Notice the null character at the end.

  • I tried all the suggestions, but it still doesn't work properly. Due to the time crunch, I have decided to use a work around:

    // global variables

    uint8_t strHeadlineNum[3] = "1";

    // local variables

    char strHeadlineName[12] = "\0";
    char strHead[5] = "Head";
    char strExtension[5] = ".wav";  

    strcat(strHeadlineName, strHead);
    strcat(strHeadlineName, strHeadlineNum);
    strcat(strHeadlineName, strExtension);

    Hopefully this helps somebody.

  • In reply to Michael Hamilton:

    I'm having a similar problem.

    I simply added a sprint to the RX62N tutorial app. But I don't see any change in my msg array after calling sprint!

    char msg[32];

    memset(msg, 0, sizeof(msg) );

    sprintf(msg, "%d message", 33);
  • In reply to MrHarmonSr:

    As DJ stated for the earlier inquiry:
    Which toolchain do you use? Which version? Which library?
    Have you added "#include <string.h>"?
    Does the compiler output any warning messages (are warnings enabled?) ?
  • In reply to FrankL:

    Adding <string.h> fixed my problem!