1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
printf/README.md

7.2 KiB

printf / sprintf for embedded systems

Build Status Coveralls Status Coverity Status Github Issues Github Releases GitHub license

This is a tiny but fully loaded printf, sprintf and snprintf implementation.
Primarily designed for usage in embedded systems, where printf is not available due to memory issues or in avoidance of linking against libc.
Using the standard libc printf may pull a lot of unwanted library stuff and can bloat code size about 20k. In this case the following implementation can be used.
Absolutely NO dependencies are required, printf.cpp brings all necessary routines, even its own fast ftoa conversion.

If memory footprint is really a critical issue, floating point support can be turned off via the PRINTF_FLOAT_SUPPORT compiler switch. When using printf (instead of sprintf) you have to provide your own _putchar() low level function as console output.

Design goals

There is a boatload of so called 'tiny' printf implementations around. So why this one?
I've tested many implementations, but most of them have very limited flag/specifier support, a lot of other dependencies or are just not standard compliant and failing the test suite. Therefore I decided to write an own implementation which meets the following items:

  • Very small implementation (< 500 code lines)
  • NO dependencies, no libs, just one module file
  • Support of all important flags, width and precision sub-specifiers (see below)
  • Support of dec/float number representation (with an own fast itoa/ftoa)
  • Reentrant and thread-safe, malloc free
  • LINT and compiler L4 warning free, clean code
  • Extensive test suite passing
  • MIT license

Usage

Add/link printf.cpp to your project and include printf.h. That's it.
Usage is 1:1 like the according stdio.h library version:

int printf(const char* format, ...);
int sprintf(char* buffer, const char* format, ...);
int snprintf(char* buffer, size_t count, const char* format, ...);

Due to genaral security reasons it is highly recommended to use snprintf (with the max buffer size as count parameter) only.

Format specifiers

A format specifier follows this prototype: %[flags][width][.precision][length]type
The following format specifiers are supported:

Supported types

Type Output
d or i Signed decimal integer
u Unsigned decimal integer
b Unsigned binary
o Unsigned octal
x Unsigned hexadecimal integer
X Unsigned hexadecimal integer (uppercase)
f Decimal floating point, lowercase
F Decimal floating point, uppercase
c Single character
s String of characters
p Pointer address
% A % followed by another % character will write a single %

Supported flags

Flags Description
- Left-justify within the given field width; Right justification is the default.
+ Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers.
By default, only negative numbers are preceded with a - sign.
(space) If no sign is going to be written, a blank space is inserted before the value.
# Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with f, F it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
0 Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).

Supported precision

Precision Description
.number For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For f and F specifiers: this is the number of digits to be printed after the decimal point. By default, this is 6, maximum is 9.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.
.* The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Supported width

Width Description
(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Supported length

The length sub-specifier modifies the length of the data type.

Length d i u o x X
(none) int unsigned int
l long int unsigned long int
ll long long int unsigned long long int

Compiler switches/defines

Name Default value Description
PRINTF_BUFFER_SIZE 128 The buffer size used for printf
NTOA_BUFFER_SIZE 32 ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number, normally 32 is a sufficient value.
FTOA_BUFFER_SIZE 32 ftoa (float) conversion buffer size. This must be big enough to hold one converted float number, normally 32 is a sufficient value.
PRINTF_FLOAT_SUPPORT defined Define this to enable floating point (%f) support

Test suite

For testing just compile, build and run the test suite located in test/test_suite.cpp. This uses the catch framework for unit-tests, which is auto-adding main().

Contributing

  1. Create an issue and describe your idea
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Publish the branch (git push origin my-new-feature)
  6. Create a new pull request
  7. Profit!

License

printf is written under the MIT license.