Sprint Library
The mikroC PRO for 8051 provides the standard ANSI C Sprintf function for easy data formatting.
Note: In addition to ANSI C standard, the Sprint Library also includes two
limited versions of the sprintf
function (sprinti
and sprintl
). These functions take less ROM and RAM and may be more convenient for use in some cases.
Functions
sprintf
Prototype |
sprintf(char *wh, const char *f,...); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
The function returns the number of characters actually written to destination string. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description |
Parameters :
The The format string is read from left to right. The first format specification encountered refers to the first argument after % [flags] [width] [.precision] [{ l | L }] conversion_type Each field in the format specification can be a single character or a number which specifies a particular format option. The
The
The The
The optional characters You must ensure that the argument type matches that of the format specification. You can use type casts to ensure that the proper type is passed to |
sprintl
Prototype |
sprintl(char *wh, const char *f,...); |
---|---|
Returns |
The function returns the number of characters actually written to destination string. |
Description |
The same as sprintf, except it doesn't support float-type numbers. |
sprinti
Prototype |
sprinti(char *wh, const char *f,...); |
---|---|
Returns |
The function returns the number of characters actually written to destination string. |
Description |
The same as sprintf, except it doesn't support long integers and float-type numbers. |
Library Example
This is a demonstration of the standard C library sprintf routine usage. Three different representations of the same floating poing number obtained by using the sprintf routine are sent via UART.
double ww = -1.2587538e+1; char buffer[15]; // Function for sending string to UART void UartWriteText(char *txt) { while(*txt) UART1_Write(*txt++); } // Function for sending const string to UART void UartWriteConstText(const char *txt) { while(*txt) UART1_Write(*txt++); } void main(){ UART1_Init(4800); // Initialize UART module at 4800 bps Delay_ms(10); UartWriteConstText("Floating point number representation"); // Write message on UART sprintf(buffer, "%12e", ww); // Format ww and store it to buffer UartWriteConstText("\r\ne format:"); // Write message on UART UartWriteText(buffer); // Write buffer on UART sprintf(buffer, "%12f", ww); // Format ww and store it to buffer UartWriteConstText("\r\nf format:"); // Write message on UART UartWriteText(buffer); // Write buffer on UART sprintf(buffer, "%12g", ww); // Format ww and store it to buffer UartWriteConstText("\r\ng format:"); // Write message on UART UartWriteText(buffer); // Write buffer on UART }
What do you think about this topic ? Send us feedback!