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
  • Again, "n" is not the problem, but anyway, here is the code and the "exact" results of the simulation on the computer, which is the same results as when I burn the chip.

    And about the size of code_char, it looked weird to me as well, but that is exactly the problem i am having, i think it keeps writing to the next position of the memory instead of going to zero.

    int oper_code;
    char code_char[5];
    
    
    void send_code (void)
    {
    
    int n;
    int p;
    n=0; p=0;
    
     if (oper_code < 1000){
     n = sprintf (code_char, "%d", 0);
     }
     n += sprintf (code_char+n, "%d", oper_code);
    
    }
    
    void main (void)
    {
      oper_code = 132;
    
      while (1)
      {
       send_code();
      }
    

    the first time: code_char = 0132
    the second time: code_char = 132132
    the third time: code_char = 132 132


    Thanks

Reply
  • Again, "n" is not the problem, but anyway, here is the code and the "exact" results of the simulation on the computer, which is the same results as when I burn the chip.

    And about the size of code_char, it looked weird to me as well, but that is exactly the problem i am having, i think it keeps writing to the next position of the memory instead of going to zero.

    int oper_code;
    char code_char[5];
    
    
    void send_code (void)
    {
    
    int n;
    int p;
    n=0; p=0;
    
     if (oper_code < 1000){
     n = sprintf (code_char, "%d", 0);
     }
     n += sprintf (code_char+n, "%d", oper_code);
    
    }
    
    void main (void)
    {
      oper_code = 132;
    
      while (1)
      {
       send_code();
      }
    

    the first time: code_char = 0132
    the second time: code_char = 132132
    the third time: code_char = 132 132


    Thanks

Children