printf

QNX SDP8.0Utilities ReferenceUtilities

Write formatted output (POSIX, toybox)

Syntax:

    printf format [argument...]
  

Runs on:

QNX OS

Options:

format
A string describing how the given arguments are to be formatted.
argument
A string to be written to standard output, under the control of format.

Description:

The printf utility formats and prints argument(s) according to format, by using the printf() syntax in the C Library Reference.

Escape sequences

The printf utility uses escape sequences in a way similar to the C programming language. The following table shows the sequences that use the backslash character (\):

This sequence: Writes:
\a An alert character (bell)
\b A backspace character
\e An escape character
\f A form-feed character
\n A newline character
\r A carriage return character
\t A tab character
\v A vertical tab character
\0 A zero character
\OCTAL The character specified by the one-, two-, or three-digit octal number OCTAL
\xHEX The character specified by the one-, or two-digit hexadecimal value HEX

Conversion character

The percent sign (%) represents characters that determine the type of conversion to be applied. The conversion characters are:
d i o u X x
The argument is an integral value. It's printed as one of the following:
  • signed integer (d or i)
  • unsigned octal (o)
  • unsigned integer (u)
  • unsigned hexadecimal (X or x)
e, E
The argument is a floating-point value, and is displayed in scientific notation. The value is printed in the style:
            [-]d[.ddd]e{+|-}dd
          

The first part is an optional sign. The second is a single digit, which is nonzero if the argument is nonzero. The third (optional) part—a decimal character followed by a number of digits equal to the specified precision—is printed if the precision is nonzero. If the precision isn't specified, it's assumed to be 6. If the precision is 0, the decimal isn't printed. The fourth part is an e or E, based upon the case of the conversion character, followed by a signed value. This value represents the order of magnitude.

f
The argument is a floating-point value. The value is printed in the style:
            [-]ddd.ddd
          

where the number of digits printed after the decimal character is equal to the specified precision. If the precision isn't specified, it's assumed to be 6. If the precision is 0, the decimal isn't printed.

g, G
The argument is a floating-point value. The value is printed in the e style (or E style if a G conversion character is given) if the exponent in this style is less than -4 or greater than or equal to the specified precision; otherwise, the value is printed in the f style.
c
The first character of the argument is printed.
s
The argument is a string of characters and the string is printed.

This utility is provided as part of the toybox package. For information on how to enable it, see toybox.

Examples:

Display the string hello, world on a line by itself:
      printf "hello, world\n"
    
Do the same as above, but don't output a newline:
      printf "hello, world"
    
Display a value in scientific notation:
      printf "n = %e\n" 3.1415926535897
    

Contributing author:

Rob Landley and the toybox project (see https://landley.net/toybox/).

Page updated: