Query: printf
OS: aix
Section: 3
Format: Original Unix Latex Style Formatted with HTML and a Horizontal Scroll Bar
Commands Reference, Volume 4, n - r
printf_Command
  Purpose
   Writes formatted output.
  Syntax
   printf Format [ Argument ... ]
  Description
   The	printf command converts, formats, and writes its Argument
parameters
   to standard output. The Argument parameters are formatted  un-
der control of
   the	Format parameter. The formatted output line cannot exceed
LINE_MAX
   bytes in length.
   The following environment variables affect  the  execution  of
the printf
   command:
	       Determines  the locale to use for the locale cate-
gories when
   LANG        both  LC_ALL  and  the  corresponding  environment
variable
	       (beginning with LC_) do not specify a locale.
	       Determines  the	locale to be used to override any
values for
   LC_ALL      locale categories specified by the setting of LANG
or any
	       other LC_ environment variable.
	       Determines  the	locale	for the interpretation of
sequences of
   LC_CTYPE    bytes of text data  as  characters;  for  example,
single versus
	       multibyte characters in parameters.
   LC_MESSAGES	Determines  the language in which messages should
be written.
	       Determines the locale for numeric formatting. This
environment
   LC_NUMERIC  variable affects the format of numbers written us-
ing the e, E,
	       f, g, and G conversion characters.
   The Format parameter is a character string that contains three
types of
   objects:
     * Plain characters copied to the output stream.
     *	Conversion  specifications, each of which cause 0 or more
items to be
       retrieved from the value parameter list.
     * The following escape sequences. When copied to the  output
stream,
       these  sequences  cause their associated action to be dis-
played on
       devices capable of the action:
          Backslash
	  Alert
	Backspace
	 Form feed
	 NCarriage return
	  Tab
	 Vertical tab
	    Where ddd is a one-, two-, or three-digit octal  num-
ber.
       dd These escape sequences are displayed as a byte with the
	    numeric value specified by the octal number.
   The	Argument parameter is a list of one or more strings to be
written to
   standard output under the control of the Format parameter.
   The Format parameter is reused as often as necessary to satis-
fy the
   Argument  parameters.  Any  extra c or s conversion specifica-
tions are
   evaluated as if a null string Argument  were  supplied;  other
extra
   conversion  specifications  are  evaluated  as if a 0 Argument
were supplied.
   Where the Format parameter contains no  conversion  specifica-
tions and
   Argument parameters are present, the results are unspecified.
   Each  conversion specification in the Format parameter has the
following
   syntax in this order:
    1. A % (percent sign).
    2. Zero or more options, which modify the meaning of the con-
version
       specification.  The  option  characters and their meanings
are:
       -     The result of the conversion is left-aligned  within
the
	     field.
       +     The result of a signed conversion always begins with
a
	     sign (+ or -).
	     If the first character of a signed conversion is not
a
       blank sign, a blank is prefixed to the result. If both the
	     blank  and  +  option characters are displayed, then
the
	     blank option character is ignored.
	     This option specifies that the value is to  be  con-
verted
	     to an alternate form. For c, d, i, u, and
	     s conversions, the option has no effect. For o
	     conversion,  it increases the precision to force the
first
	     digit of the result to be a, 0 (zero). For x and
       #     X conversions, a nonzero result has 0x, or 0X
	     prefixed to it, respectively. For e, E, f,
	     g, and G conversions, the result always contains
	     a radix character, even  if  no  digits  follow  the
radix
	     character.  For  g and G conversions, trailing zeros
are
	     not removed from the result as they usually are.
	     For d, i, o, u, x, e, E,
	     f, g, and G conversions, leading zeroes
	     (following any indication of sign or base) are  used
to
       0      pad  to  the  field width, no space padding is per-
formed. If
	     the 0 (zero) and the - (minus sign) options are
	     displayed, the 0 (zero) option is ignored. For d, i,
o,
	     u,  x,  and  X conversions, if a precision is speci-
fied,
	     the 0 (zero) option is ignored.
       Note:
       For other conversions, the behavior is undefined.
    3. An optional decimal digit string that specifies the  mini-
mum field
       width.  If  the	converted value has fewer characters than
the field
       width, the field is padded on the left to the length spec-
ified by the
       field  width.  If the left-adjustment option is specified,
the field is
       padded on the right. If the  result  of	a  conversion  is
wider than the
       field width, the field is expanded to contain the convert-
ed result. No
       truncation occurs. However, a small  precision  may  cause
truncation on
       the right.
    4. An optional precision. The precision is a . (dot) followed
by a
       decimal digit string. If no  precision  is  given,  it  is
treated as 0
       (zero). The precision specifies:
	  *  The minimum number of digits to be displayed for the
d,
	    o, i, u, x, or X conversions.
	  * The number of digits to be displayed after the  radix
character
	    for the e and f conversions.
	  * The maximum number of significant digits for the g
	    conversion.
	  *  The  maximum  number  of  bytes to be printed from a
string in the
	    s conversion.
    5. A character that indicates the type of  conversion  to  be
applied, such
       as:
       %    Performs no conversion. Prints a % (percent sign).
	    Accepts  an  integer  value and converts it to signed
decimal
	    notation. The precision specifies the minimum  number
of
	    digits  to be displayed. If the value being converted
can
	    be represented in fewer digits, it is expanded with
       d, i leading zeros. The default precision is 1. The result
of
	    converting a zero value with a precision of zero is a
null
	    string. Specifying a field width with  a  zero  as	a
leading
	    character  causes  the field width value to be padded
with
	    leading zeros.
	    Accepts an integer value and converts  it  to  signed
octal
	    notation.  The precision specifies the minimum number
of
	    digits to be displayed. If the value being	converted
can
	    be represented in fewer digits, it is expanded with
       o    leading zeros. The default precision is 1. The result
of
	    converting a zero value with a precision of zero is a
null
	    string.  Specifying  a  field  width with a zero as a
leading
	    character causes the field width value to  be  padded
with
	    leading zeros. An octal value for field width is not
	    implied.
	    Accepts an integer value and converts it to unsigned
	    decimal notation. The precision specifies the minimum
	    number of digits to be displayed. If the value being
	    converted can be represented in fewer digits, it is
       u    expanded with leading zeros. The default precision is
1.
	    The result of converting a zero value with	a  preci-
sion of
	    zero  is a null string. Specifying a field width with
a
	    zero as a leading character causes	the  field  width
value
	    to be padded with leading zeros.
	    Accepts an integer value and converts it to hexadeci-
mal
	    notation. The letters abcdef are used for the x  con-
version
	    and the letters ABCDEF are used for the X conversion.
The
	    precision specifies the minimum number of  digits  to
be
	    displayed. If the value being converted can be repre-
sented
       x, X in fewer digits, it is expanded with  leading  zeros.
The
	    default  precision	is  1. The result of converting a
zero
	    value with a precision of zero is a null string.
	    Specifying a field width with a zero as a leading
	    character causes the field width value to  be  padded
with
	    leading zeros.
	    Accepts  a	float  or double value and converts it to
decimal
	    notation in the format [-]	ddd.ddd.  The  number  of
digits
	    after the radix character (shown here as the decimal
       f    point) is equal to the precision specification. The
	    LC_NUMERIC locale category determines the radix char-
acter
	    to use tin this format. If no precision is specified,
then
	    six  digits are output. If the precision is 0 (zero),
then
	    no radix character will be displayed.
	    Accepts a float or double value and  converts  it  to
the
	    exponential form [-] d.dde{+|-}dd. There is one digit
	    before the radix character (shown here as the decimal
	    point) and the number of digits after the radix char-
acter
	    is equal to the precision specification.  The  LC_NU-
MERIC
	    locale category determines the radix character to use
tin
       e, E this format. If no precision is specified,	then  six
digits
	    are  output.  If  the  precision is 0 (zero), then no
radix
	    character will be displayed. The E conversion charac-
ter
	    produces  a number with E instead of e before the ex-
ponent.
	    The exponent always contains  at  least  two  digits.
However,
	    if	the  value  to	be  printed  requires an exponent
greater
	    than  two  digits,	additional  exponent  digits  are
printed as
	    necessary.
	    Accepts  a	float  or double value and converts it in
the
	    style of the f or e conversion characters (or
	    E in the case of the G conversion), with  the  preci-
sion
	    specifying the number of significant digits. Trailing
       g,  G zeros are removed from the result. A radix character
is
	    displayed only if it is  followed  by  a  digit.  The
style
	    used  depends on the value converted. Style g results
only
	    if the exponent resulting from the conversion is less
than
	    -4,  or  if it is greater than or equal to the preci-
sion.
       c    Accepts a value as a  string  and  prints  the  first
character
	    in the string.
	    Accepts  a	value  as  a string and prints characters
from the
	    string until the end of the string is encountered  or
the
       s    number of characters indicated by the precision is
	    reached. If no precision is specified, all characters
up
	    to the first null character are printed.
	    Accepts a value as a string, that may contain
	    backslash-escape sequences. Bytes from the converted
       b    string are printed until the end  of  the  string  or
number
	    of bytes indicated by the precision specification is
	    reached. If the precision is omitted, all bytes until
the
	    first null character are printed.
   The following backslash-escape sequences are supported:
     * The escape sequences previously listed above under the de-
scription of
       the Format parameter. These are converted to the individu-
al characters
       they represented.
     * The	  printf command to ignore any remaining  charac-
ters in the string
       parameter  containing it, any remaining string parameters,
and any
       additional characters in the Format parameter.
  Exit Status
   This command returns the following exit values:
   0			       Successful completion.
   >0			       An error occurred.
  Examples
    1. Enter the following command:
	 printf "%5d%4d0 1 21 321 4321 54321
       This produces the following output:
     1	21
   3214321
	 54321	 0
       The Format parameter is used three times to print  all  of
the given
       strings. The 0 (zero) is supplied by the printf command to
satisfy the
       last %4d conversion specification.
    2. Enter the following command:
	 printf "%c %c0 78 79
       This produces the following output:
	 7 7
    3. The following example demonstrates how the %$ format spec-
ifier can be
       used  to print the date in an order different from the or-
der of the
       arguments:
	 printf  (""%1$s,  %3$d.  %2$s,  %4$d:%5$.2d",	 weekday,
month, day, hour, min);
	 Sunday, 3. July, 10:02
 (weekday, day. month, hour:min)
  Files
   /usr/bin/printf		  Contains the printf command.
  Related Information
   The /usr/bin/echo command.
   The	printf	subroutine in AIX 5L Version 5.3 Technical Refer-
ence: Base
   Operating System and Extensions.
   The Input and Output Handling Programmer's Overview in AIX  5L
Version
   5.3	General  Programming Concepts: Writing and Debugging Pro-
grams describes
   the files,  commands,  and  subroutines  used  for  low-level,
stream, terminal,
   and asynchronous I/O interfaces.
   The	National  Language Support Overview in AIX 5L Version 5.3
National
   Language Support Guide and Reference.
________________________________________________________________________________
		      Commands Reference, Volume 4, n - r
printf_Command
  Purpose
   Writes formatted output.
  Syntax
   printf Format [ Argument ... ]
  Description
   The	printf command converts, formats, and writes its Argument
parameters
   to standard output. The Argument parameters are formatted  un-
der control of
   the	Format parameter. The formatted output line cannot exceed
LINE_MAX
   bytes in length.
   The following environment variables affect  the  execution  of
the printf
   command:
	       Determines  the locale to use for the locale cate-
gories when
   LANG        both  LC_ALL  and  the  corresponding  environment
variable
	       (beginning with LC_) do not specify a locale.
	       Determines  the	locale to be used to override any
values for
   LC_ALL      locale categories specified by the setting of LANG
or any
	       other LC_ environment variable.
	       Determines  the	locale	for the interpretation of
sequences of
   LC_CTYPE    bytes of text data  as  characters;  for  example,
single versus
	       multibyte characters in parameters.
   LC_MESSAGES	Determines  the language in which messages should
be written.
	       Determines the locale for numeric formatting. This
environment
   LC_NUMERIC  variable affects the format of numbers written us-
ing the e, E,
	       f, g, and G conversion characters.
   The Format parameter is a character string that contains three
types of
   objects:
     * Plain characters copied to the output stream.
     *	Conversion  specifications, each of which cause 0 or more
items to be
       retrieved from the value parameter list.
     * The following escape sequences. When copied to the  output
stream,
       these  sequences  cause their associated action to be dis-
played on
       devices capable of the action:
          Backslash
	  Alert
	Backspace
	 Form feed
	 NCarriage return
	  Tab
	 Vertical tab
	    Where ddd is a one-, two-, or three-digit octal  num-
ber.
       dd These escape sequences are displayed as a byte with the
	    numeric value specified by the octal number.
   The	Argument parameter is a list of one or more strings to be
written to
   standard output under the control of the Format parameter.
   The Format parameter is reused as often as necessary to satis-
fy the
   Argument  parameters.  Any  extra c or s conversion specifica-
tions are
   evaluated as if a null string Argument  were  supplied;  other
extra
   conversion  specifications  are  evaluated  as if a 0 Argument
were supplied.
   Where the Format parameter contains no  conversion  specifica-
tions and
   Argument parameters are present, the results are unspecified.
   Each  conversion specification in the Format parameter has the
following
   syntax in this order:
    1. A % (percent sign).
    2. Zero or more options, which modify the meaning of the con-
version
       specification.  The  option  characters and their meanings
are:
       -     The result of the conversion is left-aligned  within
the
	     field.
       +     The result of a signed conversion always begins with
a
	     sign (+ or -).
	     If the first character of a signed conversion is not
a
       blank sign, a blank is prefixed to the result. If both the
	     blank  and  +  option characters are displayed, then
the
	     blank option character is ignored.
	     This option specifies that the value is to  be  con-
verted
	     to an alternate form. For c, d, i, u, and
	     s conversions, the option has no effect. For o
	     conversion,  it increases the precision to force the
first
	     digit of the result to be a, 0 (zero). For x and
       #     X conversions, a nonzero result has 0x, or 0X
	     prefixed to it, respectively. For e, E, f,
	     g, and G conversions, the result always contains
	     a radix character, even  if  no  digits  follow  the
radix
	     character.  For  g and G conversions, trailing zeros
are
	     not removed from the result as they usually are.
	     For d, i, o, u, x, e, E,
	     f, g, and G conversions, leading zeroes
	     (following any indication of sign or base) are  used
to
       0      pad  to  the  field width, no space padding is per-
formed. If
	     the 0 (zero) and the - (minus sign) options are
	     displayed, the 0 (zero) option is ignored. For d, i,
o,
	     u,  x,  and  X conversions, if a precision is speci-
fied,
	     the 0 (zero) option is ignored.
       Note:
       For other conversions, the behavior is undefined.
    3. An optional decimal digit string that specifies the  mini-
mum field
       width.  If  the	converted value has fewer characters than
the field
       width, the field is padded on the left to the length spec-
ified by the
       field  width.  If the left-adjustment option is specified,
the field is
       padded on the right. If the  result  of	a  conversion  is
wider than the
       field width, the field is expanded to contain the convert-
ed result. No
       truncation occurs. However, a small  precision  may  cause
truncation on
       the right.
    4. An optional precision. The precision is a . (dot) followed
by a
       decimal digit string. If no  precision  is  given,  it  is
treated as 0
       (zero). The precision specifies:
	  *  The minimum number of digits to be displayed for the
d,
	    o, i, u, x, or X conversions.
	  * The number of digits to be displayed after the  radix
character
	    for the e and f conversions.
	  * The maximum number of significant digits for the g
	    conversion.
	  *  The  maximum  number  of  bytes to be printed from a
string in the
	    s conversion.
    5. A character that indicates the type of  conversion  to  be
applied, such
       as:
       %    Performs no conversion. Prints a % (percent sign).
	    Accepts  an  integer  value and converts it to signed
decimal
	    notation. The precision specifies the minimum  number
of
	    digits  to be displayed. If the value being converted
can
	    be represented in fewer digits, it is expanded with
       d, i leading zeros. The default precision is 1. The result
of
	    converting a zero value with a precision of zero is a
null
	    string. Specifying a field width with  a  zero  as	a
leading
	    character  causes  the field width value to be padded
with
	    leading zeros.
	    Accepts an integer value and converts  it  to  signed
octal
	    notation.  The precision specifies the minimum number
of
	    digits to be displayed. If the value being	converted
can
	    be represented in fewer digits, it is expanded with
       o    leading zeros. The default precision is 1. The result
of
	    converting a zero value with a precision of zero is a
null
	    string.  Specifying  a  field  width with a zero as a
leading
	    character causes the field width value to  be  padded
with
	    leading zeros. An octal value for field width is not
	    implied.
	    Accepts an integer value and converts it to unsigned
	    decimal notation. The precision specifies the minimum
	    number of digits to be displayed. If the value being
	    converted can be represented in fewer digits, it is
       u    expanded with leading zeros. The default precision is
1.
	    The result of converting a zero value with	a  preci-
sion of
	    zero  is a null string. Specifying a field width with
a
	    zero as a leading character causes	the  field  width
value
	    to be padded with leading zeros.
	    Accepts an integer value and converts it to hexadeci-
mal
	    notation. The letters abcdef are used for the x  con-
version
	    and the letters ABCDEF are used for the X conversion.
The
	    precision specifies the minimum number of  digits  to
be
	    displayed. If the value being converted can be repre-
sented
       x, X in fewer digits, it is expanded with  leading  zeros.
The
	    default  precision	is  1. The result of converting a
zero
	    value with a precision of zero is a null string.
	    Specifying a field width with a zero as a leading
	    character causes the field width value to  be  padded
with
	    leading zeros.
	    Accepts  a	float  or double value and converts it to
decimal
	    notation in the format [-]	ddd.ddd.  The  number  of
digits
	    after the radix character (shown here as the decimal
       f    point) is equal to the precision specification. The
	    LC_NUMERIC locale category determines the radix char-
acter
	    to use tin this format. If no precision is specified,
then
	    six  digits are output. If the precision is 0 (zero),
then
	    no radix character will be displayed.
	    Accepts a float or double value and  converts  it  to
the
	    exponential form [-] d.dde{+|-}dd. There is one digit
	    before the radix character (shown here as the decimal
	    point) and the number of digits after the radix char-
acter
	    is equal to the precision specification.  The  LC_NU-
MERIC
	    locale category determines the radix character to use
tin
       e, E this format. If no precision is specified,	then  six
digits
	    are  output.  If  the  precision is 0 (zero), then no
radix
	    character will be displayed. The E conversion charac-
ter
	    produces  a number with E instead of e before the ex-
ponent.
	    The exponent always contains  at  least  two  digits.
However,
	    if	the  value  to	be  printed  requires an exponent
greater
	    than  two  digits,	additional  exponent  digits  are
printed as
	    necessary.
	    Accepts  a	float  or double value and converts it in
the
	    style of the f or e conversion characters (or
	    E in the case of the G conversion), with  the  preci-
sion
	    specifying the number of significant digits. Trailing
       g,  G zeros are removed from the result. A radix character
is
	    displayed only if it is  followed  by  a  digit.  The
style
	    used  depends on the value converted. Style g results
only
	    if the exponent resulting from the conversion is less
than
	    -4,  or  if it is greater than or equal to the preci-
sion.
       c    Accepts a value as a  string  and  prints  the  first
character
	    in the string.
	    Accepts  a	value  as  a string and prints characters
from the
	    string until the end of the string is encountered  or
the
       s    number of characters indicated by the precision is
	    reached. If no precision is specified, all characters
up
	    to the first null character are printed.
	    Accepts a value as a string, that may contain
	    backslash-escape sequences. Bytes from the converted
       b    string are printed until the end  of  the  string  or
number
	    of bytes indicated by the precision specification is
	    reached. If the precision is omitted, all bytes until
the
	    first null character are printed.
   The following backslash-escape sequences are supported:
     * The escape sequences previously listed above under the de-
scription of
       the Format parameter. These are converted to the individu-
al characters
       they represented.
     * The	  printf command to ignore any remaining  charac-
ters in the string
       parameter  containing it, any remaining string parameters,
and any
       additional characters in the Format parameter.
  Exit Status
   This command returns the following exit values:
   0			       Successful completion.
   >0			       An error occurred.
  Examples
    1. Enter the following command:
	 printf "%5d%4d0 1 21 321 4321 54321
       This produces the following output:
     1	21
   3214321
	 54321	 0
       The Format parameter is used three times to print  all  of
the given
       strings. The 0 (zero) is supplied by the printf command to
satisfy the
       last %4d conversion specification.
    2. Enter the following command:
	 printf "%c %c0 78 79
       This produces the following output:
	 7 7
    3. The following example demonstrates how the %$ format spec-
ifier can be
       used  to print the date in an order different from the or-
der of the
       arguments:
	 printf  (""%1$s,  %3$d.  %2$s,  %4$d:%5$.2d",	 weekday,
month, day, hour, min);
	 Sunday, 3. July, 10:02
 (weekday, day. month, hour:min)
  Files
   /usr/bin/printf		  Contains the printf command.
  Related Information
   The /usr/bin/echo command.
   The	printf	subroutine in AIX 5L Version 5.3 Technical Refer-
ence: Base
   Operating System and Extensions.
   The Input and Output Handling Programmer's Overview in AIX  5L
Version
   5.3	General  Programming Concepts: Writing and Debugging Pro-
grams describes
   the files,  commands,  and  subroutines  used  for  low-level,
stream, terminal,
   and asynchronous I/O interfaces.
   The	National  Language Support Overview in AIX 5L Version 5.3
National
   Language Support Guide and Reference.
      | Similar Topics in the Unix Linux Community | 
|---|
| Multithreading in Pro*C | 
| repeat character with printf | 
| The meaning of %s in printf | 
| Need help in printf in shell script | 
| Printf statement for currency conversion |