printf

Definition: printf(integer fn, string st, object x={})
Description: Print x, to file or device fn, using format string st.
If x is an atom then all formats from st are applied to it.
If x is a sequence, then formats from st are applied to successive elements of x.
Thus printf() always takes exactly 3 arguments. However the length of the last argument, containing the values to be printed, can vary.
The basic formats are:

%s - print a sequence as a string of characters, or print an atom as a single character
%c - print an atom as a single character (nb not unicode)
%d - print an atom as a decimal integer
%x - print an atom as a hexadecimal integer (0..9 and A..F)
%X - as %x but using lower case(!!) letters (a..f)
%o - print an atom as an octal integer
%b - print an atom as a binary integer
%e - print an atom as a floating point number with exponential notation
%E - as %e but with a capital E
%f - print an atom as a floating-point number with a decimal point but no exponent
%g - print an atom as a floating point number using either the %f or %e format, whichever seems more appropriate
%G - as %g except %E instead of %e
%% - print the '%' character itself

Field widths can be added to the basic formats, e.g. %5d, %8.2f, %10.4s. The number before the decimal point is the minimum field width to be used. The number after the decimal point is the precision to be used.

For %f and %e, the precision specifies how many digits follow the decimal point character, whereas for %g, the precision specifies how many significant digits to print, and for %s the precision specifies how many characters to print.

If the field width is negative, e.g. %-5d then the value will be left-justified within the field. Normally it will be right-justified. If the field width starts with a leading 0, e.g. %08d then leading zeros will be supplied to fill up the field. If the field width starts with a '+' e.g. %+7d then a plus sign will be printed for positive values. If the field width starts with a ',' (%d and %f only) then commas are inserted every third character (phix-specific).
Comments: A statement such as printf(1,"%s","John Smith") should by rights just print 'J', as the %s should apply to the first element of x, which in this case is 'J'. The correct statement is printf(1,"%s",{"John Smith"}) where no such confusion can arise. However this is such an easy mistake to make that in phix it is caught specially and the full name printed. Note however that RDS Eu and OpenEuphoria will both just print 'J'.

Unicode is supported via UTF8 strings, which this routine treats exactly the same as ansi. %c does not support the printing of single unicode characters, but instead performs and_bits(a,#FF) and prints it as a standard ascii character.
Lastly, %x and %X are "the wrong way round" for historical/compatibility reasons.
Example 1:
balance = 12347.879
printf(myfile, "The account balance is: %,10.2f\n", balance)
      The acccount balance is:  12,347.88
Example 2:
name = "John Smith"
score = 97
printf(1, "|%15s, %5d |\n", {name, score})
      |     John Smith,    97 |
Example 3:
printf(1, "%-10.4s $ %s", {"ABCDEFGHIJKLMNOP", "XXX"})
      ABCD       $ XXX
Example 4:
printf(1, "error code %d[#%08x]", ERROR_CANCELLED)
      error code 2147943623[#800704C7]
See Also: sprintf, puts, open, and the gnu clib docs , on which the phix version is partially based, but does not use directly.
puthex32(a) and putsint(i) are low-level equivalents of printf(1,"%08x[\n]",{a}) and printf(1,"%d[\n]",{i}) respectively, see builtins\puts1[h].e