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

PROBLEMS WITH SPRINTF

Hi

i am having a problem with the sprintf function. i don't know what i am doing wrong.

i created a function where i use sprintf to separate the digits of a decimal number, and the first time that i called the function it works fine. The second time the sprintf starts when the first time left, and so on.

The variable looks something like this the first time: "0123"

and the second time: "0123123"

I don't know if i have to reset a pointer (or how to do it) so everytime i call the function it starts at the beginning of the array.



the code looks something like that:

void send_code (void)
{
int n;
	if (oper_code < 1000){
	n = sprintf (code_char, "%d", 0);
        }
	n += sprintf (code_char+n, "%d", oper_code);
  .
  .
  .
}



void main (void)
{
  .
  .
  .
while (1)
  {
  send_code();
  }
}
.

thanks in advance

Parents
  • William,

    How are you looking at the results of this function? That is, you say that these are the results even when you burn a chip. How does the hardware attached to that chip display this value to you?

    The name of the function "send_code()" suggests to me you might be trying to send this out a serial port. If you're using "char_code" as a buffer, then you might very well be overwriting it while you're trying to send it.

    When you view the results on the computer simulation, is this in the "watch" window on the char_code variable, or are you looking at the output of a serial window?

Reply
  • William,

    How are you looking at the results of this function? That is, you say that these are the results even when you burn a chip. How does the hardware attached to that chip display this value to you?

    The name of the function "send_code()" suggests to me you might be trying to send this out a serial port. If you're using "char_code" as a buffer, then you might very well be overwriting it while you're trying to send it.

    When you view the results on the computer simulation, is this in the "watch" window on the char_code variable, or are you looking at the output of a serial window?

Children