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

Parents Reply Children
  • 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"!)