14.2.7 Table of Output Conversions

Here is a table summarizing what all the different conversions do:

%d’, ‘%i

Print an integer as a signed decimal number. See Integer Conversions, for details. ‘%d’ and ‘%i’ are synonymous for output, but are different when used with scanf for input (see Table of Input Conversions).

%o

Print an integer as an unsigned octal number. See Integer Conversions, for details.

%u

Print an integer as an unsigned decimal number. See Integer Conversions, for details.

%x’, ‘%X

Print an integer as an unsigned hexadecimal number. ‘%x’ uses lowercase letters and ‘%X’ uses uppercase. See Integer Conversions, for details.

%f

Print a floating-point number in normal (fixed-point) notation. See Floating-Point Conversions, for details.

%e’, ‘%E

Print a floating-point number in exponential notation. ‘%e’ uses lowercase letters and ‘%E’ uses uppercase. See Floating-Point Conversions, for details.

%g’, ‘%G

Print a floating-point number in either normal (fixed-point) or exponential notation, whichever is more appropriate for its magnitude. ‘%g’ uses lowercase letters and ‘%G’ uses uppercase. See Floating-Point Conversions, for details.

%c

Print a single character. See Other Output Conversions.

%s

Print a string. See Other Output Conversions.

%%

Print a literal ‘%’ character. See Other Output Conversions.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so don’t do this. In particular, MATLAB allows a bare percentage sign ‘%’ with no subsequent conversion character. Octave will emit an error and stop if it sees such code. When the string variable to be processed cannot be guaranteed to be free of potential format codes it is better to use the two argument form of any of the printf functions and set the format string to %s. Alternatively, for code which is not required to be backwards-compatible with MATLAB the Octave function puts or disp can be used.

printf (strvar);        # Unsafe if strvar contains format codes
printf ("%s", strvar);  # Safe
puts (strvar);          # Safe

If there aren’t enough function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Table-of-Output-Conversions.html