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

sprintf() function behaving strange...

Hi,

I'm using uVision IDE V4.53.0.0 and the sprintf() function behaves strange... my piece of code:

_disable_irq();

len = sprintf((char*)myBuf,"%+2.2fV  %+1.3fA",9.13788,-0.004069);

_enable_irq();

This will create a string in myBuf:

"+9.14V -0.004A"

This code is called each 500ms. Suddenly after a minute or so it generates:

"-7265617330774872000000/0/0000000.000000000/0/000000000.00V +17574296777337024000/0/0000.000A"

where /0/ stands for "about 40 zero's"

Also 'len' contains a value of 66 (in stead of 15) which is the position of the first '.' in the resulting string.

As all interrupts are disabled the variables cannot be overwritten by some interrupt handler.

Does anybody know?

Thanks,

Henk

Parents
  • Hi,

    That's a good question.
    Let me explain:

    I can't remember if a char or int is by default signed or unsigned.
    I think it depends on the compiler.
    Also it depends on the platform if an int is 16-bit or 32 bit.

    So I decided to convert all standard variable types to unsigned and signed followed by the bit width using typedefs.
    All the typedefs I use are:
    u08, u16, u32 for unsigned
    s08, s16, s32 for signed

    so my u08 is of type uint8_t.

    myBuf is declared as u08 (uint8_t).

    The compiler generates a warning when using u08 with sprintf.
    Therefore I cast it to the requested char*.

    Henk

Reply
  • Hi,

    That's a good question.
    Let me explain:

    I can't remember if a char or int is by default signed or unsigned.
    I think it depends on the compiler.
    Also it depends on the platform if an int is 16-bit or 32 bit.

    So I decided to convert all standard variable types to unsigned and signed followed by the bit width using typedefs.
    All the typedefs I use are:
    u08, u16, u32 for unsigned
    s08, s16, s32 for signed

    so my u08 is of type uint8_t.

    myBuf is declared as u08 (uint8_t).

    The compiler generates a warning when using u08 with sprintf.
    Therefore I cast it to the requested char*.

    Henk

Children