Format specifier¶
Syntax¶
Full syntax for format specifier is
%[flags][width][.precision][length]type
Flags¶
Flags field may have zero or more characters, and in any order. List of supported flags:
Character |
Description |
---|---|
minus |
Left-align the output of this placeholder. The default is to right-align the output |
plus |
Prepends a plus for positive signed-numeric types.
positive = |
space `` `` |
Prepends a space for positive signed-numeric types.
positive = `` |
zero |
When the width option is specified, prepends zeros for numeric types. The default prepends spaces, if this flag is not set |
apostrophe |
The integer or exponent of a decimal has the thousands grouping separator applied. |
has |
Alternate form:
For |
Width¶
Width field specifies a minimum number of characters to output, and is typically used to pad fixed-width fields in tabulated output, where fields would otherwise be smaller. Please keep in mind that this parameter does not truncate output is input is longer than width field value.
Concerning width field, you may:
Ignore it completely, output does not rely on width field by any means
Write a fixed value as part of format specifier. Number must be an integer value
Use asterisk
*
char and pass number as part of parameter.printf("%3d", 6)
orprintf("%*d", 3, 6)
will generate the same output.
Tip
When fixed value is used to set width field, leading zero is not counted as part of width field, but as flag instead, indicating prepend number with leading zeros
Precision¶
Precision field usually specifies a maximum limit on the output, depending on the particular formatting type. For floating point numeric types, it specifies the number of digits to the right of the decimal point that the output should be rounded. For the string type, it limits the number of characters that should be output, after which the string is truncated.
Precision field may be omitted, or a numeric integer value, or a dynamic value when passed as another argument when indicated by an asterisk *
. For example, printf("%.*s", 3, "abcdef")
will result in abc
being printed.
Length¶
Length field may be ignored or one of the below:
Character |
Description |
---|---|
|
For integer types, causes |
|
For integer types, causes |
|
For integer types, causes |
|
For integer types, causes |
|
For floating point types, causes |
|
For integer types, causes |
|
For integer types, causes |
|
For integer types, causes |
Specifier types¶
This is a list of standard specifiers for outputting the data to the stream. Column Supported gives an overview which specifiers are actually supported by the library.
Specifier |
Supported |
Description |
---|---|---|
|
Yes |
Prints literal |
|
Yes |
Prints |
|
Yes |
Prints |
|
Yes |
Prints |
|
Yes |
Prints |
|
Yes |
Prints |
|
Yes |
Prints |
|
Yes |
Prints |
|
Yes |
Prints null terminated string |
|
Yes |
Prints |
|
Yes |
Prints |
|
Not yet |
Prints |
|
Yes |
Prints nothing but writes the number of characters successfully written so far into an integer pointer parameter |
Notes about float types¶
It is important to understand how library works under the hood to understand limitations on floating-point numbers.
When it comes to level of precision, maximum number of digits is linked to support long
or long long
integer types.
Note
When long long
type is supported by the compiler (usually part of C99 or later),
maximum number of valid digits is 18
, or 9
digits if system supports only long
data types.
If application tries to use more precision digits than maximum, remaining digits are automatically printed as all 0
.
As a consequence, output using LwPRINTF library may be different in comparison to other printf
implementations.
Tip
Float data type supports up to 7
and double up to 15
.
Additional specifier types¶
LwPRINTF implementation supports some specifiers that are usually not available in standard implementation. Those are more targeting embedded systems although they may be used in any general-purpose application
Specifier |
Description |
---|---|
|
Prints |
|
Prints |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /* List of specifiers added in the library which are not available in standard printf implementation */
#include "lwprintf/lwprintf.h"
/**
* \brief List of additional specifiers to print
*/
void
additional_format_specifiers(void) {
unsigned char my_array[] = { 0x01, 0x02, 0xA4, 0xB5, 0xC6 };
/* Binary output */
/* Prints number 8 in binary format, so "1000" */
lwprintf_printf("%b\r\n", 8U);
/* Prints number 16 in binary format with 10 places, so " 10000" */
lwprintf_printf("%10b\r\n", 16U);
/* Prints number 16 in binary format with 10 places, leading zeros, so "0000010000" */
lwprintf_printf("%010b\r\n", 16U);
/* Array outputs */
/* Fixed length with uppercase hex numbers, outputs "0102A4B5C6" */
lwprintf_printf("%5K\r\n", my_array);
/* Fixed length with lowercase hex numbers, outputs "0102a4b5c6" */
lwprintf_printf("%5k\r\n", my_array);
/* Variable length with uppercase letters, outputs "0102A4B5C6" */
lwprintf_printf("%*K\r\n", (int)LWPRINTF_ARRAYSIZE(my_array), my_array);
/* Variable length with lowercase letters, outputs "0102a4b5c6" */
lwprintf_printf("%*k\r\n", (int)LWPRINTF_ARRAYSIZE(my_array), my_array);
/* Variable length with uppercase letters and spaces, outputs "01 02 A4 B5 C6" */
lwprintf_printf("% *K\r\n", (int)LWPRINTF_ARRAYSIZE(my_array), my_array);
/* Variable length with uppercase letters and spaces, outputs "01 02 a4 b5 c6" */
lwprintf_printf("% *k\r\n", (int)LWPRINTF_ARRAYSIZE(my_array), my_array);
}
|