Discussion Forum

sprintf

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Holger Warzelhan
Posted
9-Mar-2001 15:32 GMT
Toolset
C51
New! 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
Read-Only
Author
Alex Ruiz
Posted
9-Mar-2001 16:29 GMT
Toolset
C51
New! RE: sprintf
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


- Alex

Read-Only
Author
Holger Warzelhan
Posted
9-Mar-2001 17:16 GMT
Toolset
C51
New! RE: sprintf
Tried that, but it still doesn't do it.
I can even do it simpler:
sprintf (command, "Test");
There is no change done to the variable "command" by sprintf, no
matter what kind of format I use.
I know it should work, I use the same commands on a unix system with
gcc, that's just the first time I use it with C51.
Please help!
Holger
Read-Only
Author
Alex Ruiz
Posted
9-Mar-2001 17:51 GMT
Toolset
C51
New! RE: sprintf
char i=100;
sprintf (command, "Test %c", i); // command has changed

It does it, but not in the way you want to show it ( 100 in string format ), in your situation typecast is really the correct choice.

- Alex
Read-Only
Author
Holger Warzelhan
Posted
9-Mar-2001 18:43 GMT
Toolset
C51
New! RE: sprintf
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");
using C166, the value of command is: "Just a text".
using C51, the value of command is still the same as before, and didn't
get changed by sprintf.
Same result with any possible format string in sprintf.
Can there be something wrong with my C51 version?
Thanks again for your time!
Holger
Read-Only
Author
Alex Ruiz
Posted
9-Mar-2001 19:00 GMT
Toolset
C51
New! RE: sprintf
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

Read-Only
Author
Holger Warzelhan
Posted
9-Mar-2001 19:06 GMT
Toolset
C51
New! RE: sprintf
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
Read-Only
Author
Alex Ruiz
Posted
9-Mar-2001 19:13 GMT
Toolset
C51
New! RE: sprintf
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.
Read-Only
Author
Jon Ward
Posted
9-Mar-2001 18:38 GMT
Toolset
C51
New! RE: sprintf
How do you "know" it doesn't work? What are you using to test it?

Jon
Read-Only
Author
Holger Warzelhan
Posted
9-Mar-2001 18:47 GMT
Toolset
C51
New! RE: sprintf
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
Read-Only
Author
Jon Ward
Posted
9-Mar-2001 20:56 GMT
Toolset
C51
New! RE: sprintf
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
Read-Only
Author
Holger Warzelhan
Posted
9-Mar-2001 21:45 GMT
Toolset
C51
New! RE: sprintf
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");
using C166, the value of command is: "Just a text".
using C51, the value of command is still "Hello", and didn't get changed
by sprintf.
Same result with any possible format string in sprintf.
Can there be something wrong with my C51 version?
Holger
Read-Only
Author
Jon Ward
Posted
10-Mar-2001 22:39 GMT
Toolset
C51
New! RE: sprintf
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

Read-Only
Author
Mark Odell
Posted
12-Mar-2001 15:32 GMT
Toolset
C51
New! RE: sprintf
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
Read-Only
Author
Jon Ward
Posted
12-Mar-2001 20:13 GMT
Toolset
C51
New! RE: sprintf
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
Read-Only
Author
Andrew Neil
Posted
10-Mar-2001 10:54 GMT
Toolset
C51
New! RE: sprintf
"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?
Read-Only
Author
Mark Odell
Posted
9-Mar-2001 17:18 GMT
Toolset
C51
New! RE: sprintf
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);

- Mark
Read-Only
Author
Sergey
Posted
10-Mar-2001 14:57 GMT
Toolset
C51
New! No problem with sprintf()
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"

See advice of Mark Odell about 'integer promotion'.

Read-Only
Author
David Turnbull
Posted
10-Mar-2001 15:02 GMT
Toolset
C51
New! RE: sprintf
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);
Read-Only
Author
Andrew Neil
Posted
12-Mar-2001 09:29 GMT
Toolset
C51
New! RE: sprintf
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"!)
Read-Only
Author
Hans-Herbert Kirste
Posted
12-Mar-2001 08:52 GMT
Toolset
C51
New! RE: sprintf
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.
Read-Only
Author
Holger Warzelhan
Posted
14-Mar-2001 19:37 GMT
Toolset
C51
New! RE: sprintf
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

Next Thread | Thread List | Previous Thread Start a Thread | Settings