Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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
#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;
}
sprintf (buffer," %d:%d:%d:%d ", hr, min, s, ms);
Lcd_Write_String(1,buffer);
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..?
sprintf (buffer," %02d:%02d:%02d:%03d ", hr, min, s, ms);
Lcd_Write_String(1,buffer);
comparing printf to sprintf, is print better..?
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().
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;
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.