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(); } }
hi, could you explain more what is wrong there? I see the only error is not initialized variable n. Maybe it helps:
void send_code (void) { int n = 0;
Hi I am sorry, i should have said it, I posted only part of the code. The variable is initialized at n=0, but anyway, that is not the problem, the "n" is the returned value of the function, and it is the number of characters written to the buffer this is the definition of the variable at the beginning of the proigram, maybe it will help.
int bdata oper_code; sbit LSB_oper_code = oper_code^0; char code_char[5]; //char* pCode_char = code_char; // pointer
hi, the "n" is the returned value of the function Not always. n is correctly initialized only if oper_code below 1000. If at first time you call function send_code() with value above 999 then not initialized n will be used in next line:
n += sprintf (code_char+n, "%d", oper_code);