NAME

zkFprintf, zkPrintf, zkSnprintf, zkSprintf - print formatted output

SYNOPSIS

#include "zkStdio.h"

int zkFprintf(FILE* stream, const char* format, ...);
int zkPrintf(const char* format, ...);
int zkSnprintf(char* s, size_t n, const char* format, ...);
int zkSprintf(char* s, const char* format, ...);
 

DESCRIPTION

The zkFprintf() function places output on the named output stream. The zkPrintf() function places output on the standard output stream stdout. The zkSprintf() function places output followed by the null byte, '\0', in consecutive bytes starting at *s; it is the user's responsibility to ensure that enough space is available.

zkSnprintf() is identical to zkSprintf() with the addition of the n argument, which states the size of the buffer referred to by s.

Each of these functions converts, formats and prints its arguments under control of the format. The format is a character string, beginning and ending in its initial shift state, if any. The format is composed of zero or more directives: ordinary characters, which are simply copied to the output stream and conversion specifications, each of which results in the fetching of zero or more arguments. The results are undefined if there are insufficient arguments for the format. If the format is exhausted while arguments remain, the excess arguments are evaluated but are otherwise ignored.

All forms of the zkFprintf() functions allow for the insertion of a language-dependent radix character in the output string. The radix character is defined in the program's locale (category LC_NUMERIC). In the C locale, or in a locale where the radix character is not defined, the radix character defaults to a period (.).

Each conversion specification is introduced by the % character, after which the following appear in sequence:

A field width, or precision, or both, may be indicated by an asterisk (*). In this case an argument of type int supplies the field width or precision. Arguments specifying field width, or precision, or both must appear in that order before the argument, if any, to be converted. A negative field width is taken as a - flag followed by a positive field width. A negative precision is taken as if the precision were omitted.

The flag characters and their meanings are:

-
The result of the conversion will be left-justified within the field. The conversion will be right-justified if this flag is not specified.
+
The result of a signed conversion will always begin with a sign (+ or -). The conversion will begin with a sign only when a negative value is converted if this flag is not specified.
space
If the first character of a signed conversion is not a sign or if a signed conversion results in no characters, a space will be prefixed to the result. This means that if the space and + flags both appear, the space flag will be ignored.
0
For d, i, o, u, x, X, e, E, f, g and G conversions, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed. If the 0 and - flags both appear, the 0 flag will be ignored. For d, i, o, u, x and X conversions, if a precision is specified, the 0 flag will be ignored. If the 0 and ' flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behaviour is undefined.

The conversion characters and their meanings are:

d, i
The int argument is converted to a signed decimal in the style [-]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters.
o
The unsigned int argument is converted to unsigned octal format in the style dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters.
u
The unsigned int argument is converted to unsigned decimal format in the style dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters.
x
The unsigned int argument is converted to unsigned hexadecimal format in the style dddd; the letters abcdef are used. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters.
X
Behaves the same as the x conversion character except that letters ABCDEF are used instead of abcdef.
f
The double argument is converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the radix character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly 0 and no # flag is present, no radix character appears. If a radix character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. The zkFprintf() family of functions may make available character string representations for infinity and NaN.
e, E
The double argument is converted in the style [-]d.ddde±dd, where there is one digit before the radix character (which is non-zero if the argument is non-zero) and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is 0 and no # flag is present, no radix character appears. The value is rounded to the appropriate number of digits. The E conversion character will produce a number with E instead of e introducing the exponent. The exponent always contains at least two digits. If the value is 0, the exponent is 0. The zkFprintf() family of functions may make available character string representations for infinity and NaN.
g, G
The double argument is converted in the style f or e (or in the style E in the case of a G conversion character), with the precision specifying the number of significant digits. If an explicit precision is 0, it is taken as 1. The style used depends on the value converted; style e (or E) will be used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result; a radix character appears only if it is followed by a digit. The zkFprintf() family of functions may make available character string representations for infinity and NaN.
c
The int argument is converted to an unsigned char, and the resulting byte is written.
s
The argument must be a pointer to an array of char. Bytes from the array are written up to (but not including) any terminating null byte. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array must contain a null byte.
p
The argument must be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-dependent manner.
n
The argument must be a pointer to an integer into which is written the number of bytes written to the output so far by this call to one of the zkFprintf() functions. No argument is converted.
%
Print a %; no argument is converted. The entire conversion specification must be %%.

If a conversion specification does not match one of the above forms, the behaviour is undefined.

In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is simply expanded to contain the conversion result. Characters generated by zkFprintf() and zkPrintf() are printed as if zkFputc() had been called.

RETURN VALUE

Upon successful completion, these functions return the number of bytes transmitted excluding the terminating null in the case of zkSprintf() or zkSnprintf() or a negative value if an output error was encountered.

If the value of n is zero on a call to zkSnprintf(), an unspecified value less than 1 is returned.

ERRORS

For the conditions under which zkFprintf() and zkPrintf() will fail and may fail, refer to zkFputc()

In addition, all forms of zkFprintf() may fail if:

[EILSEQ]
A wide-character code that does not correspond to a valid character has been detected.
[EINVAL]
There are insufficient arguments.

In addition, zkFprintf() and zkPrintf() may fail if:

[ENOMEM]
Insufficient storage space is available.