Continue to Site

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.

  • 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.

Formatting string using sprintf?

Status
Not open for further replies.

MrNobody

New Member
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
 
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

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:
#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
 
Thanks.. I'm using C18 for PIC18F4550..
Actually, its for LCD.. I'm trying to do a stopwatch using PIC..
Code:
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..
 
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..
 
Thanks.. I'm using C18 for PIC18F4550..
Actually, its for LCD.. I'm trying to do a stopwatch using PIC..
Code:
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..?

Yes. It would look something like this:

Code:
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. :)

comparing printf to sprintf, is print better..?

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
 
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..

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
 
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.
 
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().

This may be of interest.

MPLAB® C18 C COMPILER LIBRARIES
4.7.1 Output Streams
Output is based on the use of a destination stream. A stream can be a peripheral,
memory buffer, or any other consumer of data and is denoted by a pointer to an object
of FILE type. MPLAB C18 defines two streams in the standard library:
_H_USER output via the user-defined output function _user_putc.
_H_USART output via the library output function _usart_putc.
The current version of the library supports only these two output streams. Both streams are always considered to be open and do not require use of functions such as fopen, fclose, etc.

The global variables stdout and stderr are defined by the library and have default value of _H_USART. To change the destination to be _H_USER, assign that value to the variable. For example, to change standard output to use the user defined output function:
stdout = _H_USER;

Printf sends a formatted string output to stdout by default.
 
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.

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 **broken link removed**, 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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top