This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sprintf

Hello!

I am trying to use sprintf, but get no result (C51, version 5.20).

char xdata command [80], i=100;
strcpy (command, "Hello!"); // now command contains "Hello!"
sprintf (command, "Test %d", i); // command has not changed
Why would sprintf not work?
Thank you for any help!
Holger

  • OK, I got it to work just fine. Here's what I did.

    1. Create a new project.

    2. Select the Intel 8052 from the device database.

    3. Add the following file to the project:

    #include <stdio.h>
    #include <string.h>
    
    char xdata command [80];
    
    void main (void)
    {
    strcpy (command, "Hello");
    sprintf (command, "Just a text");
    
    while (1)
      {
      }
    }
    

    4. Compile and link. Note that I set no compiler or linker options.

    5. Start the debugger and set a watchpoint on command (type ws command in the command window).

    6. Single-step thru the program. I see command get set to "hello" and then set to "Just a text".

    If you repeat these steps do you still have trouble?

    Jon

  • Have you ever checked the pre-processor output of C51 ? Make sure that the pre-processor does not replace the text "sprintf" with anything else.

  • char i;
    :
    sprintf(command, "%bd", (char)i);

    Having used the "%bd", you don't also need the (char) cast;
    The "%bd" specifies that it expects a single byte.

    You only need to do either:
    sprintf(command, "%bd", i);
    or:
    sprintf(command, "%d", (int)i);
    See the section "Problems Using the printf Routines" in Appendix F of the C51 User's Guide (p350 of the 03.2000 version).
    Also, lots of the code examples for the library routines illustrate the use of the "%b" - just search the PDF for "%b" !

    (NB: The font used to display messages on this forum makes the percent symbol '%' look like the number "96"!)

  • Just a note Jon, while (1); may cause compilers to emit a diagnostic. Using for(;;); will not. I've been caught replacing while's with for's in projects where no warnings were permitted at the highest warning level.

    A boring point, I admit.

    Regards.

    - Mark

  • Yeah,

    Back in the old days,

    while (1);

    actually generated code with many compilers while

    for(;;);

    did not.

    I mainly use while(1) because it's immediately obvious what it does.

    I interview about 2 people a week who claim to be C programmers who cannot tell me what for(;;) does!!! So, I try not to give an answer that generates even MORE questions. :-)

    Jon

  • Hi,

    ok, it's working (again).
    There must have been a problem with my installation of the C51
    Compiler. After reinstallation sprintf works fine!

    Thanks to all of you for your help!
    Holger