We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
How do you "know" it doesn't work? What are you using to test it? Jon
Jon, I display the content of my variable before and after the sprintf command. We have 2 similar devices, one using an 8bit C51 program, the newer one using a 16bit C166 program. C166 works fine, but the same sprintf commands in C51 don't seem to affect the variable that sprintf is supposed to change (I can do a bunch of sprintf commands without changing the initial value of my variable). Holger
Can you write a very small example of sprintf failing? I use sprintf a lot. I mean a whole lot and never have problems. Jon
Jon, What I initialy did is I took a function I wrote using C166 that uses a bunch of sprintf commands, and copied it into my C51 source code. Now, my sprintf commands that work fine with C166 don't work with C51. Is there a major difference? After executing the lines:
char xdata command [80]; strcpy (command, "Hello"); sprintf (command, "Just a text");
"I display the content of my variable before and after the sprintf command." Are you sure that it's the sprintf not working; could it be a problem with your display routine? Have you tried it on the simulator, so that you can watch exactly what's going on? Do you use optimisation? If so, have you tried disabling it?
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) { } }
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);
for(;;);