xref: /csrg-svn/usr.bin/printf/printf.1 (revision 62183)
1*62183Sbostic.\" Copyright (c) 1989, 1990, 1993
2*62183Sbostic.\"	The Regents of the University of California.  All rights reserved.
336726Sbostic.\"
450202Sbostic.\" This code is derived from software contributed to Berkeley by
550202Sbostic.\" the Institute of Electrical and Electronics Engineers, Inc.
650202Sbostic.\"
750524Scael.\" %sccs.include.redist.roff%
836726Sbostic.\"
9*62183Sbostic.\"	@(#)printf.1	8.1 (Berkeley) 06/06/93
1036726Sbostic.\"
1143083Scael.Dd
1243083Scael.Dt PRINTF 1
1350524Scael.Os
1443083Scael.Sh NAME
1543083Scael.Nm printf
1643083Scael.Nd formatted output
1743083Scael.Sh SYNOPSIS
1843083Scael.Nm printf format
1950524Scael.Op arguments  ...
2043083Scael.Sh DESCRIPTION
2143083Scael.Nm Printf
2236726Sbosticformats and prints its arguments, after the first, under control
2336726Sbosticof the
2443083Scael.Ar format  .
2536726SbosticThe
2643083Scael.Ar format
2736726Sbosticis a character string which contains three types of objects: plain characters,
2836726Sbosticwhich are simply copied to standard output, character escape sequences which
2936726Sbosticare converted and copied to the standard output, and format specifications,
3036726Sbosticeach of which causes printing of the next successive
3143083Scael.Ar argument  .
3243083Scael.Pp
3336726SbosticThe
3443083Scael.Ar arguments
3536726Sbosticafter the first are treated as strings if the corresponding format is
3636726Sbosticeither
3743083Scael.Cm c
3836726Sbosticor
3950524Scael.Cm s ;
4036726Sbosticotherwise it is evaluated as a C constant, with the following extensions:
4143083Scael.Pp
4250524Scael.Bl -bullet -offset indent -compact
4350524Scael.It
4436726SbosticA leading plus or minus sign is allowed.
4550524Scael.It
4636726SbosticIf the leading character is a single or double quote, or not a digit,
4736726Sbosticplus, or minus sign, the value is the ASCII code of the next character.
4850524Scael.El
4943083Scael.Pp
5036726SbosticThe format string is reused as often as necessary to satisfy the
5143083Scael.Ar arguments  .
5236726SbosticAny extra format specifications are evaluated with zero or the null
5336726Sbosticstring.
5443083Scael.Pp
5536726SbosticCharacter escape sequences are in backslash notation as defined in the
5650524Scaeldraft proposed
5750524Scael.Tn ANSI C
5850524ScaelStandard
5950524Scael.Tn X3J11 .
6050524ScaelThe characters and their meanings
6136726Sbosticare as follows:
6250524Scael.Bl -tag -width Ds -offset indent
6350524Scael.It Cm \ea
6436727SbosticWrite a <bell> character.
6550524Scael.It Cm \eb
6636726SbosticWrite a <backspace> character.
6750524Scael.It Cm \ef
6836726SbosticWrite a <form-feed> character.
6950524Scael.It Cm \en
7036726SbosticWrite a <new-line> character.
7150524Scael.It Cm \er
7236726SbosticWrite a <carriage return> character.
7350524Scael.It Cm \et
7436726SbosticWrite a <tab> character.
7550524Scael.It Cm \ev
7636726SbosticWrite a <vertical tab> character.
7750524Scael.It Cm \e\'
7836727SbosticWrite a <single quote> character.
7950524Scael.It Cm \e\e
8036726SbosticWrite a backslash character.
8150524Scael.It Cm \e Ns Ar num
8250524ScaelWrite an 8-bit character whose
8350524Scael.Tn ASCII
8450524Scaelvalue is the 1-, 2-, or 3-digit
8536726Sbosticoctal number
8650524Scael.Ar num .
8750524Scael.El
8843083Scael.Pp
8936726SbosticEach format specification is introduced by the percent character
9036726Sbostic(``%'').
9150524ScaelThe remainder of the format specification includes,
9250524Scaelin the following order:
9350524Scael.Bl -tag -width Ds
9450524Scael.It "Zero or more of the following flags:"
9550524Scael.Bl -tag -width Ds
9650524Scael.It Cm #
9743083ScaelA `#' character
9836726Sbosticspecifying that the value should be printed in an ``alternate form''.
9943083ScaelFor
10043083Scael.Cm c  ,
10143083Scael.Cm d ,
10236726Sbosticand
10343083Scael.Cm s  ,
10436726Sbosticformats, this option has no effect.  For the
10543083Scael.Cm o
10636726Sbosticformats the precision of the number is increased to force the first
10736726Sbosticcharacter of the output string to a zero.  For the
10843083Scael.Cm x
10943083Scael.Pq Cm X
11036726Sbosticformat, a non-zero result has the string
11143083Scael.Li 0x
11243083Scael.Pq Li 0X
11343083Scaelprepended to it.  For
11443083Scael.Cm e  ,
11543083Scael.Cm E ,
11643083Scael.Cm f  ,
11743083Scael.Cm g ,
11836726Sbosticand
11943083Scael.Cm G  ,
12036726Sbosticformats, the result will always contain a decimal point, even if no
12136726Sbosticdigits follow the point (normally, a decimal point only appears in the
12236726Sbosticresults of those formats if a digit follows the decimal point).  For
12343083Scael.Cm g
12436726Sbosticand
12543083Scael.Cm G
12636726Sbosticformats, trailing zeros are not removed from the result as they
12736726Sbosticwould otherwise be;
12850524Scael.It Cm \&\-
12943083ScaelA minus sign `\-' which specifies
13043083Scael.Em left adjustment
13136726Sbosticof the output in the indicated field;
13250524Scael.It Cm \&+
13343083ScaelA `+' character specifying that there should always be
13436726Sbostica sign placed before the number when using signed formats.
13550524Scael.It Sq \&\ \&
13643083ScaelA space specifying that a blank should be left before a positive number
13736726Sbosticfor a signed format.  A `+' overrides a space if both are used;
13850524Scael.It Cm \&0
13943083ScaelA zero `0' character indicating that zero-padding should be used
14036726Sbosticrather than blank-padding.  A `\-' overrides a `0' if both are used;
14150524Scael.El
14250524Scael.It "Field Width:"
14343083ScaelAn optional digit string specifying a
14443083Scael.Em field width ;
14536726Sbosticif the output string has fewer characters than the field width it will
14636726Sbosticbe blank-padded on the left (or right, if the left-adjustment indicator
14736726Sbostichas been given) to make up the field width (note that a leading zero
14836726Sbosticis a flag, but an embedded zero is part of a field width);
14950524Scael.It Precision:
15043083ScaelAn optional period,
15143083Scael.Sq Cm \&.\& ,
15243083Scaelfollowed by an optional digit string giving a
15343083Scael.Em precision
15436726Sbosticwhich specifies the number of digits to appear after the decimal point,
15543083Scaelfor
15643083Scael.Cm e
15743083Scaeland
15843083Scael.Cm f
15943083Scaelformats, or the maximum number of characters to be printed
16036726Sbosticfrom a string; if the digit string is missing, the precision is treated
16136726Sbosticas zero;
16250524Scael.It Format:
16343083ScaelA character which indicates the type of format to use (one of
16443083Scael.Cm diouxXfwEgGcs ) .
16550524Scael.El
16643083Scael.Pp
16743083ScaelA field width or precision may be
16843083Scael.Sq Cm \&*
16943083Scaelinstead of a digit string.
17036726SbosticIn this case an
17143083Scael.Ar argument
17236726Sbosticsupplies the field width or precision.
17343083Scael.Pp
17436726SbosticThe format characters and their meanings are:
17550524Scael.Bl -tag -width Fl
17650524Scael.It Cm diouXx
17736726SbosticThe
17843083Scael.Ar argument
17936729Sbosticis printed as a signed decimal (d or i), unsigned decimal, unsigned octal,
18036729Sbosticor unsigned hexadecimal (X or x), respectively.
18150524Scael.It Cm f
18236726SbosticThe
18343083Scael.Ar argument
18443083Scaelis printed in the style `[\-]ddd.ddd' where the number of d's
18536726Sbosticafter the decimal point is equal to the precision specification for
18636726Sbosticthe argument.
18744957ScaelIf the precision is missing, 6 digits are given; if the precision
18844957Scaelis explicitly 0, no digits and no decimal point are printed.
18950524Scael.It Cm eE
19036726SbosticThe
19143083Scael.Ar argument
19243083Scaelis printed in the style
19343083Scael.Cm e
19450524Scael.`[-]d.ddd Ns \(+-dd\'
19543083Scaelwhere there
19636726Sbosticis one digit before the decimal point and the number after is equal to
19736726Sbosticthe precision specification for the argument; when the precision is
19844957Scaelmissing, 6 digits are produced.
19936726SbosticAn upper-case E is used for an `E' format.
20050524Scael.It Cm gG
20136726SbosticThe
20243083Scael.Ar argument
20336726Sbosticis printed in style
20443083Scael.Cm f
20536726Sbosticor in style
20643083Scael.Cm e
20743083Scael.Pq Cm E
20836726Sbosticwhichever gives full precision in minimum space.
20950524Scael.It Cm c
21036726SbosticThe first character of
21143083Scael.Ar argument
21236726Sbosticis printed.
21350524Scael.It Cm s
21436726SbosticCharacters from the string
21543083Scael.Ar argument
21636726Sbosticare printed until the end is reached or until the number of characters
21736726Sbosticindicated by the precision specification is reached; however if the
21836726Sbosticprecision is 0 or missing, all characters in the string are printed.
21950524Scael.It Cm \&%
22036726SbosticPrint a `%'; no argument is used.
22150524Scael.El
22243083Scael.Pp
22336726SbosticIn no case does a non-existent or small field width cause truncation of
22436726Sbostica field; padding takes place only if the specified field width exceeds
22536726Sbosticthe actual width.
22650524Scael.Sh RETURN VALUES
22743083Scael.Nm Printf
22836811Sbosticexits 0 on success, 1 on failure.
22943083Scael.Sh SEE ALSO
23043083Scael.Xr printf 3
23143083Scael.Sh HISTORY
23250524ScaelThe
23350524Scael.Nm printf
23450524Scaelcommand appeared in
23550524Scael.Bx 4.3 Reno .
23650524ScaelIt is modeled
23744957Scaelafter the standard library function,
23844957Scael.Xr printf 3 .
23943083Scael.Sh BUGS
24050524ScaelSince the floating point numbers are translated from
24150524Scael.Tn ASCII
24250524Scaelto floating-point and
24336731Sbosticthen back again, floating-point precision may be lost.
24443083Scael.Pp
24550524Scael.Tn ANSI
24650524Scaelhexadecimal character constants were deliberately not provided.
247