1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Formatting string using sprintf?

Discussion in 'Microcontrollers' started by MrNobody, Aug 25, 2008.

  1. MrNobody

    MrNobody New Member

    Joined:
    Aug 29, 2006
    Messages:
    251
    Likes:
    3
    Hi,
    Lets say I have a int variable for example int hr = 1;. If i use sprintf (buffer,"%i", hr), the buffer result would be '1', a single digit value. If I want to make it into 2 digit such as "01", how should i do it..?
    Please advice..

    Thanks
     
  2. Torben

    Torben Well-Known Member

    Joined:
    Oct 6, 2006
    Messages:
    2,507
    Likes:
    34
    Location:
    B.C., Canada
    If you're on *nix or *BSD then "man sprintf" will give you everything you need, assuming your dev docs are installed.

    If you're not so lucky then Googling "sprintf" will turn up many pages containing the formatting rules.

    For this example, what you are looking for is "%02i", as in:

    Code (text):

    #include <stdio.h>

    int main(int argc, char* argv[])
    {
        int i = 0;

        for (i = 1; i <= 10; i++) {
            printf("Line %02i\n", i);
        }

        return 0;
    }
     
    Note that if you're coding for a microcontroller, using sprintf() can increase your program code size significantly.


    Torben
     
  3. MrNobody

    MrNobody New Member

    Joined:
    Aug 29, 2006
    Messages:
    251
    Likes:
    3
    Thanks.. I'm using C18 for PIC18F4550..
    Actually, its for LCD.. I'm trying to do a stopwatch using PIC..
    Code (text):

    sprintf (buffer,"  %d:%d:%d:%d   ", hr, min, s, ms);
    Lcd_Write_String(1,buffer);
    That is to print the value onto the LCD but the text is not formatted correctly because some values are 1 digit and some are 2 digits etc..

    can the code u showed me use in this scenario..?
    comparing printf to sprintf, is print better..?

    please advice.. thanks..
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. MrNobody

    MrNobody New Member

    Joined:
    Aug 29, 2006
    Messages:
    251
    Likes:
    3

    Actually, the LCD driver wasn't written by me.. and the author uses sprintf.. when i tried to change it to printf, it doesn't work.. so i have to use sprintf i guess.. btw the code u showed me works provided i use sprintf.. thanks..
     
  6. Torben

    Torben Well-Known Member

    Joined:
    Oct 6, 2006
    Messages:
    2,507
    Likes:
    34
    Location:
    B.C., Canada
    Yes. It would look something like this:

    Code (text):

    sprintf (buffer,"  %02d:%02d:%02d:%03d   ", hr, min, s, ms);
    Lcd_Write_String(1,buffer);
    The first '0' (zero) after the '%' indicates that the value should be zero-padded. The number following that '0' says what the full width of the printed number should be. Above I've chosen widths of 2 (i.e. '01, '02', '22', etc.) except for milliseconds, where you need a width of 3.

    There are many other options and rules for using sprintf formats. I strongly suggest that you Google 'sprintf' and read the first link. Also, the output format specifiers are fully documented in the C18 library manual, section 4.7.2, under 'fprintf()' (which is another function which uses the same formatting).

    The beginning of section 4.7 contains some other information about this family of functions which may be of interest to you. For instance, floating-point specifiers are not supported. This probably won't be important in your current project but might be in the future. :)

    They use the same code internally, so the answer to that depends entirely upon the situation. If you want to print directly to where-ever the standard output is going, then printf() is probably easier. If you just want to easily format a string for use within the program or for printing to another output, then you are pretty much going to have to use sprintf() like you have in your example.

    Of course, C18 might have facilities for redirecting printf()'s output to your LCD--I have never used PICs so I don't know. I have reason to believe I may be using them shortly, however. :)


    Torben
     
  7. Torben

    Torben Well-Known Member

    Joined:
    Oct 6, 2006
    Messages:
    2,507
    Likes:
    34
    Location:
    B.C., Canada
    Sorry, I was doing something else and didn't see that you'd posted this until after I replied. I am not surprised that you needed to use sprintf()--I'm not sure but I don't think C18 redirects printf() output anywhere special by default. So you'd need sprintf().

    Anyway, I'm glad it's working for you. Good luck with your project!


    Torben
     
  8. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    I'm curious as to why you don't need the cast to far rom in your code?

    sprintf (buffer,(const far rom char*)" %02d:%02d:%02d:%03d ", hr, min, s, ms);

    Is there some way to make this automatic or did you leave that part out as it's pic specific.

    Mike.
     
  9. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    This may be of interest.

    Printf sends a formatted string output to stdout by default.
     
  10. Torben

    Torben Well-Known Member

    Joined:
    Oct 6, 2006
    Messages:
    2,507
    Likes:
    34
    Location:
    B.C., Canada
    Hi Mike,

    I'm not sure who you're talking to here. If it's MrNobody then I'll let him answer that one. If it's me, then I left it out since a) as I said I've never used a PIC so wouldn't know that, and b) yeah, it's PIC-specific.

    At any rate, according to this thread on Microchip's forums, a more suitable solution to including that monstrous cast every time would be to simply either recompile the libraries to the correct memory model for the chip being used or else just compile using the large memory model.

    Apparently the compiler is smart enough to emit the correct code anyway, just printing a warning as it does so.

    Again, never even held a PIC in my hands but may be doing so soon. ;) If the above is a load of dingos' kidneys please set me straight.


    Cheers,

    Torben
     

Share This Page