Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Teensy 2 - sprintf - datatypes for INT?

  1. #1
    Junior Member
    Join Date
    Feb 2013

    Red face Teensy 2 - sprintf - datatypes for INT?


    I just recently ordered a teensy 2, after weeks of troubles with a fake one (w/o knowing! Bummer).
    Now, it's just plain fun!

    Yet, it's been over 10 years since I tried programming C in a decent way.
    Being new in chips & microcontrollers, so to understand hardware-programming, I decided to try Arduino-teensyduino-interface first.

    After blinking leds, I managed to get my teensy talking over Serial & Uart, having conversations with an old windows-based gps.
    But, while using sprintf to build some strings, I wonder about performance & datatypes.

    char *SSTRING;
    int Data1=0, Data2=0, Data3=0;

    loop {

    Data1=999999; // Need to be able to send at least 6 "9"s.

    sprintf(SSTRING,"$SOFT,%u,%i,%u",Data1,Data2,Data3 );



    Not entirely okay, but something like that, you get the point.

    Now, it's sending all kind of stuff, but the problem is that "999999" gets turned in some number that doesn't reflect at all what I'm trying to send, turned in something like 19382, or -32000 and a bit... Probably just overflowed?

    So, I've got a couple of questions:

    1. What am I doing wrong? Which datatype is supported in teensy and what am I supposed to use to print integers like 999999 (6 9s)? int? double? long?
    I don't need floatingpoint-possibilities, just positive numbers. And some negatives but those are quite smaller (temperature-like).

    2. Am I actually using the correct flags for sprintf (%u, %i) to build my string?

    3. Performance-wise, is there a better option than sprintf to build my string? And get it over the Uart?

    4. Is it okay to use char* for my send-string or would it be better to just use a fixed char[xxxx]-size-thing? Ending it with "\0"?

    Pretty newbie-questions, I know... but after trying some different things with doubles, longs & floats, I seem to be doing something wrong.
    Thanks in advance.

    Last edited by fré; 02-27-2013 at 11:41 AM.

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Ayer Massachussetts
    Ints on AVR ATmegas like the Teensy 2.0 are only 16-bits. This means that you can only represent -32768 to +32767. You could use long which is 32-bits. However, I believe the sprintf in the library may not be able to handle long data types. If it does you would use sprintf (buffer, "%lu, %ld, %lu"). Whether you use %i or %d is up to you (%i was added to make scanf support multiple bases, and it was added to printf as well).

  3. #3
    Senior Member Jp3141's Avatar
    Join Date
    Nov 2012
    Or as MichaelMeissner also pointed out in another post, there is a siprint() function which--if you don't need floating point--is smaller.

    Also, your
    char *SSTRING;
    doesn't actually allocate any space for the string -- you'll get strange hard-to-detect bugs.
    char SSTRING[]="123456";

    will allocate space for 6 characters (plus the \0 at the end)

  4. #4
    Junior Member
    Join Date
    Feb 2013

    Seems like I managed to fix some things... So, thank you for that.
    Float & Long's get parsed totally absurdly with sprintf, so I tried using other types.
    When defining my vars with "int32_t" instead of "long", I can print the numbers I need, in combination with the right "%lu"'s.

    I'm just not too sure it's the right solution for my problem, but hey, it works. (Supposing Arduino does handle float & long the way it should for other functions & libraries.)

    Happily working at my next issues...
    Last edited by fré; 03-01-2013 at 01:32 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts