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. It is important to point out that the printf function does not add a newline (or carriage return) to the output by default. That can lead to some confusing behavior if you do not know what to expect. For example, the command printf('Hello') does not appear to produce any output. In fact, it does produce the text, but it then gets overwritten by the prompt. To see the text, you need printf('Hello\n'). This seems odd, but allows you to assemble a line using multiple printf commands, including the '\n' when you are done with the line. You can also use the '\r' character as an explicit carriage return (with no line feed). This allows you to write to the same line many times (to show a progress string, for example).

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\n',3,1.53);
intvalue is 3, floatvalue is 1.530000

Next, we print out a string value.

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

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

--> printf('integer padded is %012d\n',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\n',pi);
float value is +0003.141592653590