SYNOPSIS

int printf(const char *format, /* args*/ ...);
int sprintf(char *s, const char *format, /* args*/ ...);

The printf() function places output on the output stream stdout. The sprintf() function places output, followed by the null byte (\0), in consecutive bytes starting at s; the CScript engine ensures that enough storage is available.
Each of these functions converts, formats, and prints its args 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 defined as follows:

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. Conversions can be applied to the nth argument after the format in the argument list, rather than to the next unused argument. In this case, the conversion character % (see below) is replaced by the sequence %n$, where n is a decimal integer in the range [1, NL_ARGMAX], giving the position of the argument in the argument list. This feature provides for the definition of format strings that select arguments in an order appropriate to specific languages (see the EXAMPLES section).

In format strings containing the %n$ form of conversion specifications, numbered arguments in the argument list can be referenced from the format string as many times as required.

In format strings containing the % form of conversion specifications, each argument in the argument list is used exactly once.

Escape Sequences
The following escape sequences produce the associated action on display devices capable of the action:

Conversion Specifications
Each conversion specification is introduced by the character % or by the character sequence %n$, after which the following appear in sequence:

A field width or precision may be indicated by an asterisk (*) instead of a digit string. In this case, an integer args supplies the field width or precision. The args that is actually converted is not fetched until the conversion letter is seen, so the args specifying field width or precision must appear before the args (if any) to be converted. If the precision argument is negative, it will be changed to zero. A negative field width argument is taken as a - flag, followed by a positive field width. A negative precision is taken as if the precision were omitted. In format strings containing the %n$ form of a conversion specification, a field width or precision may be indicated by the sequence *m$, where m is a decimal integer in the range [1, NL_ARGMAX] giving the position in the argument list (after the format argument) of an integer argument containing the field width or precision, for example:

printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);

The format can contain either numbered argument specifications (that is, %n$ and *m$), or unnumbered argument specifications (that is, % and *), but normally not both. The only exception to this is that %% can be mixed with the %n$ form. The results of mixing numbered and unnumbered argument specifications in a format string are undefined. When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string.

Flag Characters
The flag characters and their meanings are:

Conversion Characters
Each conversion character results in fetching zero or more args. The results are undefined if there are insufficient args for the format. If the format is exhausted while args remain, the excess args are ignored.

The conversion characters and their meanings are:

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

If a floating-point value is the internal representation for infinity, the output is [_]Infinity, where Infinity is either Infinity or Inf, depending on the desired output string length. Printing of the sign follows the rules described above.

If a floating-point value is the internal representation for "not-a-number," the output is [_]NaN. Printing of the sign follows the rules described above.

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 printf() are printed as if the function had been called.

RETURN VALUES

The printf() and sprintf() functions return the number of bytes transmitted (not including the \0 in the case of sprintf()). The snprintf() function returns the number of characters formatted, that is, the number of characters that were written to the buffer. Each function returns a negative value if an output error was encountered.

EXAMPLES

Example 1: To print the language-independent date and time format, the following statement could be used:
printf (format, weekday, month, day, hour, min);
For American usage, format could be a pointer to the string:
"%s, %s %d, %d:%.2d\n"
producing the message:
Sunday, July 3, 10:02
whereas for German usage, format could be a pointer to the string:
"%1$s, %3$d. %2$s, %4$d:%5$.2d\n"
producing the message:
Sonntag, 3. Juli, 10:02
Example 2: To print a date and time in the form Sunday, July 3, 10:02, where weekday and month are pointers to null-terminated strings: printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min); Example 3: To print pi to 5 decimal places: printf("pi = %.5f", 4 * atan(1.0));