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
Why would sprintf not work? It's working. If you want to see the value 100 ( 64h 'd' ) you need to change the format string argument, %d means int, if you put %c it will work as expected because you are using a char data type.
char xdata command [80], i=100; sprintf (command, "Test %c", i); // command has changed
Tried that, but it still doesn't do it. I can even do it simpler:
sprintf (command, "Test");
With ANSI integer promotion disabled (typical) you must cast the char to an int.
char total = 100; sprintf(buf, "Total is %d\n", (int) total);
char i=100; sprintf (command, "Test %c", i); // command has changed
How do you "know" it doesn't work? What are you using to test it? Jon
Hi Alex, Thank you for your help. It is a bit hard to explain without listing a large amount of source code here. 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? I would have thought that's an ANSI command and it should work the same way in every C compiler version. After executing the line:
sprintf (command, "Just a text");
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
I would have thought that's an ANSI command and it should work the same way in every C compiler version. This command is ANSI C patern. It's really strange the fact that you can't see it working. What is the simulator that you're attempting to watch it? - Alex
I am not using a simulator, I test the software directly with our custom hardware (works since a few years, no problem on that side), and show the variable on an attached display. Holger
Sorry but, in this case, if the code works out of the hardware ( simulation only ) I think an Emulator may help you catch what is going on with your hardware.
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?
All work OK (compiled and simulated in dScope). But without explicitly typecasting of 'i' you get wrong result
char xdata command [80], i=100; sprintf (command, "Test %d", (int) i); // command has "Test 100" sprintf (command, "Test %d", i); // command has "Test 25600"
The variable i is defined as a char (1 byte). sprintf "%d" expects an integer (2 bytes). Try casting it usually works for me, and use the b to say its a byte. sprintf(command, "%bd", (char)i);