Draft Writeup of April 22, 1996


The command
	printf format-string, arglist ;
and the function
	sprintf ( format-string, arglist )
expand the format-string by substitution from the arglist, and send the result to standard output or return the result to the calling expression, respectively.

Most characters are copied verbatim from the format-string to the output. Conversion specifications, which start with a % and end with a format letter, are an exception. Each conversion specification is replaced by one argument from the arglist, formatted in a characteristic way as follows:

%d signed decimal notation
%u unsigned decimal notation
%o unsigned octal notation, without leading 0
%x unsigned hexadecimal, using abcdef, without leading 0x
%X unsigned hexadecimal, using ABCDEF, without leading 0X
%c single character
%s character string, not surrounded by quotes
%q character string, surrounded by quotes if and only if an AMPL data statement would require them
%Q character string, surrounded by quotes
%f double-precision floating-point
%e double-precision floating-point, exponential notation using e
%E double-precision floating-point, exponential notation using E
%g double-precision floating-point, using %f or %e
%G double-precision floating-point, using %f or %E
%% literal % (not a conversion: consumes no arguments)
Between the % and the format letter there may be any of the following:
Symbol Interpretation
- left-justify
+ display a sign (+ or -)
0 pad with leading zeroes
w place in a field at least w characters wide
.d in %s, %q, %Q: print at most d characters from the string
in %d: print at least d digits, adding leading zeroes if necessary
in %f, %e: print d digits after the decimal point
in %g: print d significant digits
Field widths w and precisions d are either decimal numbers or a *, which is replaced by the value of the next item in the arglist. Each conversion specification consumes one or (when * 's are involved) more items from the arglist and formats the last item it consumes.

If the precision d is omitted, it is taken as zero. In the case of %g, a precision of 0 (%.0g or %.g) specifies "full" precision: the shortest decimal string that rounds to the value being formatted. If no . appears, %g is treated as %.6g. Analogous rules apply to %G.

The standard C escape sequences are allowed: \a (alert or bell), \b (backspace), \f (formfeed), \n (newline), \r (carriage return), \t (horizontal tab), \v (vertical tab), \xd or \xdd, where d denotes a hexadecimal digit, and \0d, \0dd, or \0ddd, where d denotes an octal digit.

Comments or questions?
Write to info@ampl.com
or use our comment form.

Return to the character strings writeup.

Return to the AMPL update page.

Return to the AMPL home page.