How it works

LwPRINTF library supports 2 different formatting output types:

  • Write formatted data to user input array

  • Directly print formatted characters by calling output_function for every formatted character in the input string

Text formatting is based on input format string followed by the data parameters. It is mostly used to prepare numeric data types to human readable format.

Note

LwPRINTF is open-source implementation of regular stdio.h library in C language. It implements only output functions, excluding input scanning features

Formatting functions take input format string followed by (optional) different data types. Internal algorithm scans character by character to understand type of expected data user would like to have printed.

Every format specifier starts with letter %, followed by optional set of flags, widths and other sets of characters. Last part of every specifier is its type, that being type of format and data to display.

Tip

To print number 1234 in human readable format, use specifier %d. With default configuration, call lwprintf_printf("%d", 1234); and it will print "1234".

Check section Format specifier for list of all formats and data types

Character output function

API functions printing characters directly to the output stream (ex. lwprintf_printf), require output function to be set during initialization procedure.

Output function is called by the API for every character to be printed/transmitted by the application.

Note

Output function is set during initialization procedure.

If not set (set as NULL), it is not possible to use API function which directly print characters to output stream. Application is then limited only to API functions that write formatted data to input buffer.

Notes to consider:

  • Output function must return same character as it was used as an input parameter to consider successful print

  • Output function will receive (int)'\0' character to indicate no more characters will follow in this API call

  • Single output function may be used for different LwPRINTF instances

Absolute minimum example to support direct output
 1#include "lwprintf/lwprintf.h"
 2
 3/* Called for every character to be printed */
 4int
 5lwprintf_out(int ch, lwprintf_t* lwp) {
 6    /* May use printf to output it for test */
 7    if (ch != '\0') {
 8    	printf("%c", (char)ch);
 9    }
10
11    return ch;
12}
13
14int
15main(void) {
16    /* Initialize default lwprintf instance with output function */
17    lwprintf_init(lwprintf_out);
18
19    /* Print first text */
20    lwprintf_printf("Text: %d", 10);
21}