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?

Threaded View

  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 12:41 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