21.19.2 printf

The printf statement gets a format string and, optionally, a list of values to print. It can be invoked using two alternative syntaxes, which are equivalent:

printf (fmt[, value…])
printf fmt[, value…]

The format string fmt is printed verbatim to the standard output, but for format tags which start with %. These format tags are interpreted especially. Most of the format tags “consume” one of the specified values. Every value in the list shall be described by a tag, or the compiler will signal an error. Likewise, values without a corresponding describing tag is an error. These tags are:

%s

Print the argument as a string.

%ibits(d|x|o|b|c)

Print the argument as a signed integer of size bits. The last letter determines how the argument is printed.

d

Print the integer in decimal.

x

Print the integer in hexadecimal.

o

Print the integer in octal.

b

Print the integer in binary.

c

Print the integer as an ASCII character. This only works with 8 bit integers.

%u

Same as %i, but for unsigned integers.

%c

A shorter way to write %u8c.

%fbits[.precision]d
%ebits[.precision]d
%gbits[.precision]d

These tags accept integral values, that are cast to an integral value of width bits and then interpreted as an IEEE 754 floating-point number. Currently, valid values for bits are 32 and 64.

When using %f tag, the number will be printed in decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. The default precision for %f32 tag is 7 and for %f64 is 15.

The %e tag will print the number in the style [-]d.ddde+-dd where there is one digit (which is non-zero if the floating-point number is non-zero) before the decimal-point character and the number of digits after it is equal the precision. The exponent always contains at least two digits; if the value is zero, the exponent is 00.

And %g tag will print the number in style %f or %e. The precision specifies the number of the significant digits. Style %e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.

(poke) printf ("%f64d\n", 0x40091eb851eb851fUL);
3.140000000000000
(poke) printf ("pi:%e32d\n", 0x4048f5c3U)
pi:3.1400001e+00
%v

Print the value printed representation of the argument, which can be of any type including complex types like arrays and structs. This is similar to the write operation available in many Lisp systems.

This tag supports an optional numerical argument (restricted to a single digit) that specifies the maximum depth level when printing structures. Example:

(poke) printf ("%1v\n", struct { s = struct { i = 10 },  l = 20L });
struct {s=struct {…},l=0x14L}

By default, the depth level is 0, which means no limit.

This tag also support an optional flag that specifies how the value is printed. The supported tags are F (for flat) and T (for tree). The default is flat. Example:

(poke) printf ("%Tv\n", struct { s = struct { i = 10 },  l = 20L });
struct {
  s=struct {
    i=0xa
  },
  l=0x14L
}
(poke) printf ("%Fv\n", struct { s = struct { i = 10 },  l = 20L });
struct {s=struct {i=0xa},l=0x14L}

This tag is mainly intended to be used in pretty-printers.

The following format tags do not consume arguments. They support emitting styled text using the libtextstyle approach of having styling classes that you can customize in a .css file.

%<classname:

Start the styling class with name classname. The class name cannot be empty.

%>

End the last opened styling class. All styling classes should be ended before finishing the format string.

%%

Print % character.

Note that styling classes can be nested, but all classes should be ended before finishing the format string.

If you use a name class, you can define how to style it in the .css file (poke installs and uses poke-default.css but you can set the POKE_STYLE environment variable to point to another css) like this:

.NAME { text-decoration: blink; color : pink; }

Examples:

(poke) printf "This is a NAME: %<NAME:xxx%>"
This is a NAME: xxx
(poke) printf "Name: %<string:%s%> Age: %<integer:%i32d%>, "Jose", 39
Name: Jose Age: 39