PRINTF Formated Output Function (C-Style)

Section: Input/Ouput Functions

Usage

Prints values to the output. The general syntax for its use is
  printf(format,a1,a2,...)

Here format is the format string, which is a string that controls the format of the output. The values of the variables a_i are substituted into the output as required. It is an error if there are not enough variables to satisfy the format string. Note that this printf command is not vectorized! Each variable must be a scalar.

Format of the format string

The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, and an optional precision. The arguments must correspond properly (after type promotion) with the conversion specifier, and are used in the order given.

The flag characters

The character % is followed by zero or more of the following flags:

The field width

An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). A negative field width is taken as a '-' flag followed by a positive field width. 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 expanded to contain the conversion result.

The precision

An optional precision, in the form of a period ('.') followed by an optional decimal digit string. If the precision is given as just '.', or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix character for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s conversions.

The conversion specifier

A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are:

Example

Here are some examples of the use of printf with various arguments. First we print out an integer and double value.
--> printf('intvalue is %d, floatvalue is %f
',3,1.53);
intvalue is 3, floatvalue is 1.530000

Next, we print out a string value.

--> printf('string value is %s
','hello');
string value is hello

Now, we print out an integer using 12 digits, zeros up front.

--> printf('integer padded is %012d
',32);
integer padded is 000000000032

Print out a double precision value with a sign, a total of 18 characters (zero prepended if necessary), a decimal point, and 12 digit precision.

--> printf('float value is %+018.12f
',pi);
float value is +0003.141592653590

inserted by FC2 system