home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / ixemul-39.47-env-bin.lha / man / cat3 / printf.0 < prev    next >
Text File  |  1993-12-07  |  15KB  |  265 lines

  1.  
  2. PRINTF(3)                  UNIX Programmer's Manual                  PRINTF(3)
  3.  
  4. NNAAMMEE
  5.      pprriinnttff, ffpprriinnttff, sspprriinnttff, ssnnpprriinnttff, vvpprriinnttff, vvffpprriinnttff,, vvsspprriinnttff,
  6.      vvssnnpprriinnttff - formatted output conversion
  7.  
  8. SSYYNNOOPPSSIISS
  9.      ##iinncclluuddee <<ssttddiioo..hh>>
  10.  
  11.      _i_n_t
  12.      pprriinnttff(_c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _._._.)
  13.  
  14.      _i_n_t
  15.      ffpprriinnttff(_F_I_L_E _*_s_t_r_e_a_m, _c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _._._.)
  16.  
  17.      _i_n_t
  18.      sspprriinnttff(_c_h_a_r _*_s_t_r, _c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _._._.)
  19.  
  20.      _i_n_t
  21.      ssnnpprriinnttff(_c_h_a_r _*_s_t_r, _s_i_z_e___t _s_i_z_e, _c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _._._.)
  22.  
  23.      ##iinncclluuddee <<ssttddaarrgg..hh>>
  24.  
  25.      _i_n_t
  26.      vvpprriinnttff(_c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _v_a___l_i_s_t _a_p)
  27.  
  28.      _i_n_t
  29.      vvffpprriinnttff(_F_I_L_E _*_s_t_r_e_a_m, _c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _v_a___l_i_s_t _a_p)
  30.  
  31.      _i_n_t
  32.      vvsspprriinnttff(_c_h_a_r _*_s_t_r, _c_h_a_r _*_f_o_r_m_a_t, _v_a___l_i_s_t _a_p)
  33.  
  34.      _i_n_t
  35.      vvssnnpprriinnttff(_c_h_a_r _*_s_t_r, _s_i_z_e___t _s_i_z_e, _c_o_n_s_t _c_h_a_r _*_f_o_r_m_a_t, _v_a___l_i_s_t _a_p)
  36.  
  37. DDEESSCCRRIIPPTTIIOONN
  38.      The pprriinnttff() family of functions produces output according to a _f_o_r_m_a_t as
  39.      described below.  PPrriinnttff() and vvpprriinnttff() write output to _s_t_d_o_u_t_, the
  40.      standard output stream; ffpprriinnttff() and vvffpprriinnttff() write output to the giv­
  41.      en output _s_t_r_e_a_m; sspprriinnttff(), ssnnpprriinnttff(), vvsspprriinnttff(), and vvssnnpprriinnttff()
  42.      write to the character string _s_t_r. These functions write the output under
  43.      the control of a _f_o_r_m_a_t string that specifies how subsequent arguments
  44.      (or arguments accessed via the variable­length argument facilities of
  45.      stdarg(3))  are converted for output.  These functions return the number
  46.      of characters printed (not including the trailing `\0' used to end output
  47.      to strings).  SSnnpprriinnttff() and vvssnnpprriinnttff() will write at most _s_i_z_e-1 of the
  48.      characters printed into the output string (the _s_i_z_e'th character then
  49.      gets the terminating `\0'); if the return value is greater than or equal
  50.      to the _s_i_z_e argument, the string was too short and some of the printed
  51.      characters were discarded.  SSpprriinnttff() and vvsspprriinnttff() effectively assume
  52.      an infinte _s_i_z_e.
  53.  
  54.      The format string is composed of zero or more directives: ordinary char­
  55.      acters (not %%), which are copied unchanged to the output stream; and con­
  56.      version specifications, each of which results in fetching zero or more
  57.      subsequent arguments.  Each conversion specification is introduced by the
  58.      character %%. The arguments must correspond properly (after type promo­
  59.      tion) with the conversion specifier.  After the %%, the following appear
  60.      in sequence:
  61.  
  62.      oo   Zero or more of the following flags:
  63.  
  64.                --   a ## character specifying that the value should be converted
  65.                    to an ``alternate form''.  For cc, dd, ii, nn, pp, ss, and uu,
  66.                    conversions, this option has no effect.  For oo conversions,
  67.                    the precision of the number is increased to force the first
  68.                    character of the output string to a zero (except if a zero
  69.                    value is printed with an explicit precision of zero).  For
  70.                    xx and XX conversions, a non­zero result has the string `0x'
  71.                    (or `0X' for XX conversions) prepended to it.  For ee, EE, ff,
  72.                    gg, and GG, conversions, the result will always contain a
  73.                    decimal point, even if no digits follow it (normally, a
  74.                    decimal point appears in the results of those conversions
  75.                    only if a digit follows).  For gg and GG conversions, trail­
  76.                    ing zeros are not removed from the result as they would
  77.                    otherwise be.
  78.  
  79.                --   A zero `00' character specifying zero padding.  For all con­
  80.                    versions except nn, the converted value is padded on the
  81.                    left with zeros rather than blanks.  If a precision is giv­
  82.                    en with a numeric conversion (Mc d, ii, oo, uu, ii, xx, and XX),
  83.                    the `00' flag is ignored.
  84.  
  85.                --   A negative field width flag `--' indicates the converted
  86.                    value is to be left adjusted on the field boundary.  Except
  87.                    for nn conversions, the converted value is padded on the
  88.                    right with blanks, rather than on the left with blanks or
  89.                    zeros.  A `--' overrides a `00' if both are given.
  90.  
  91.                --   A space, specifying that a blank should be left before a
  92.                    positive number produced by a signed conversion (dd, ee, EE,
  93.                    ff, gg, GG, or ii).
  94.  
  95.                --   a `++' character specifying that a sign always be placed be­
  96.                    fore a number produced by a signed conversion.  A `++' over­
  97.                    rides a space if both are used.
  98.  
  99.      oo   An optional decimal digit string specifying a minimum field width.
  100.          If the converted value has fewer characters than the field width, it
  101.          will be padded with spaces on the left (or right, if the left­
  102.          adjustment flag has been given) to fill out the field width.
  103.  
  104.      oo   An optional precision, in the form of a period `..' followed by an op­
  105.          tional digit string.  If the digit string is omitted, the precision
  106.          is taken as zero.  This gives the minimum number of digits to appear
  107.          for dd, ii, oo, uu, xx, and XX conversions, the number of digits to appear
  108.          after the decimal­point for ee, EE, and ff conversions, the maximum num­
  109.          ber of significant digits for gg and GG conversions, or the maximum
  110.          number of characters to be printed from a string for ss conversions.
  111.  
  112.      oo   The optional character hh, specifying that a following dd, ii, oo, uu, xx,
  113.          or XX conversion corresponds to a _s_h_o_r_t _i_n_t or _u_n_s_i_g_n_e_d _s_h_o_r_t _i_n_t ar­
  114.          gument, or that a following nn conversion corresponds to a pointer to
  115.          a _s_h_o_r_t _i_n_t argument.
  116.  
  117.      oo   The optional character ll (ell) specifying that a following dd, ii, oo,
  118.          uu, xx, or XX conversion applies to a pointer to a _l_o_n_g _i_n_t or _u_n_s_i_g_n_e_d
  119.          _l_o_n_g _i_n_t argument, or that a following nn conversion corresponds to a
  120.          pointer to a _l_o_n_g _i_n_t argument.
  121.  
  122.      oo   The character LL specifying that a following ee, EE, ff, gg, or GG conver­
  123.          sion corresponds to a _l_o_n_g _d_o_u_b_l_e argument (but note that long double
  124.          values are not currently supported by the VAX and Tahoe compilers).
  125.  
  126.      oo   A character that specifies the type of conversion to be applied.
  127.  
  128.      A field width or precision, or both, may be indicated by an asterisk `*'
  129.      instead of a digit string.  In this case, an _i_n_t argument supplies the
  130.      field width or precision.  A negative field width is treated as a left
  131.      adjustment flag followed by a positive field width; a negative precision
  132.      is treated as though it were missing.
  133.  
  134.      The conversion specifiers and their meanings are:
  135.  
  136.      ddiioouuxxXX  The _i_n_t (or appropriate variant) argument is converted to signed
  137.              decimal (dd and ii), unsigned octal (oo), unsigned decimal (uu), or
  138.              unsigned hexadecimal (xx and XX) notation.  The letters aabbccddeeff are
  139.              used for xx conversions; the letters AABBCCDDEEFF are used for conver­
  140.              sions.  The precision, if any, gives the minimum number of digits
  141.              that must appear; if the converted value requires fewer digits,
  142.              it is padded on the left with zeros.
  143.  
  144.      DDOOUU     The _l_o_n_g _i_n_t argument is converted to signed decimal, unsigned
  145.              octal, or unsigned decimal, as if the format had been lldd, lloo, or
  146.              lluu respectively.  These conversion characters are deprecated, and
  147.              will eventually disappear.
  148.  
  149.      eeEE      The _d_o_u_b_l_e argument is rounded and converted in the style
  150.              [-]d..dddee+­dd where there is one digit before the decimal­point
  151.              character and the number of digits after it is equal to the pre­
  152.              cision; if the precision is missing, it is taken as 6; if the
  153.              precision is zero, no decimal­point character appears.  An EE con­
  154.              version uses the letter EE (rather than ee) to introduce the expo­
  155.              nent.  The exponent always contains at least two digits; if the
  156.              value is zero, the exponent is 00.
  157.  
  158.      ff       The _d_o_u_b_l_e argument is rounded and converted to decimal notation
  159.              in the style [­]ddd..ddd, where the number of digits after the
  160.              decimal­point character is equal to the precision specification.
  161.              If the precision is missing, it is taken as 6; if the precision
  162.              is explicitly zero, no decimal­point character appears.  If a
  163.              decimal point appears, at least one digit appears before it.
  164.  
  165.      gg       The _d_o_u_b_l_e argument is converted in style ff or ee (or EE for GG con­
  166.              versions).  The precision specifies the number of significant
  167.              digits.  If the precision is missing, 6 digits are given; if the
  168.              precision is zero, it is treated as 1.  Style ee is used if the
  169.              exponent from its conversion is less than ­4 or greater than or
  170.              equal to the precision.  Trailing zeros are removed from the
  171.              fractional part of the result; a decimal point appears only if it
  172.              is followed by at least one digit.
  173.  
  174.      cc       The _i_n_t argument is converted to an _u_n_s_i_g_n_e_d _c_h_a_r, and the re­
  175.              sulting character is written.
  176.  
  177.      ss       The ``_c_h_a_r _*'' argument is expected to be a pointer to an array
  178.              of character type (pointer to a string).  Characters from the ar­
  179.              ray are written up to (but not including) a terminating NUL char­
  180.              acter; if a precision is specified, no more than the number spec­
  181.              ified are written.  If a precision is given, no null character
  182.              need be present; if the precision is not specified, or is greater
  183.              than the size of the array, the array must contain a terminating
  184.              NUL character.
  185.  
  186.      pp       The ``_v_o_i_d _*'' pointer argument is printed in hexadecimal (as if
  187.              by `%#x' or `%#lx').
  188.  
  189.      nn       The number of characters written so far is stored into the inte­
  190.              ger indicated by the ``_i_n_t _*'' (or variant) pointer argument.  No
  191.              argument is converted.
  192.  
  193.      %%       A `%' is written. No argument is converted. The complete conver­
  194.              sion specification is `%%'.
  195.  
  196.      In no case does a non­existent or small field width cause truncation of a
  197.      field; if the result of a conversion is wider than the field width, the
  198.      field is expanded to contain the conversion result.
  199.  
  200. EEXXAAMMPPLLEESS
  201.      To print a date and time in the form `Sunday, July 3, 10:02', where
  202.      _w_e_e_k_d_a_y and _m_o_n_t_h are pointers to strings:
  203.  
  204.            #include <stdio.h>
  205.            fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",
  206.                    weekday, month, day, hour, min);
  207.  
  208.      To print pi to five decimal places:
  209.  
  210.            #include <math.h>
  211.            #include <stdio.h>
  212.            fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0));
  213.  
  214.      To allocate a 128 byte string and print into it:
  215.  
  216.            #include <stdio.h>
  217.            #include <stdlib.h>
  218.            #include <stdarg.h>
  219.            char *newfmt(const char *fmt, ...)
  220.            {
  221.                            char *p;
  222.                            va_list ap;
  223.                            if ((p = malloc(128)) == NULL)
  224.                                    return (NULL);
  225.                            va_start(ap, fmt);
  226.                            (void) vsnprintf(p, 128, fmt, ap);
  227.                            va_end(ap);
  228.                            return (p);
  229.            }
  230.  
  231. SSEEEE AALLSSOO
  232.      printf(1),  scanf(3)
  233.  
  234. SSTTAANNDDAARRDDSS
  235.      The ffpprriinnttff(), pprriinnttff(), sspprriinnttff(), vvpprriinnttff(), vvffpprriinnttff(), and vvsspprriinnttff()
  236.      functions conform to ANSI C3.159­1989 (``ANSI C'').
  237.  
  238. HHIISSTTOORRYY
  239.      The functions ssnnpprriinnttff() and vvssnnpprriinnttff() are new to this release.
  240.  
  241. BBUUGGSS
  242.      The conversion formats %%DD, %%OO, and %%UU are not standard and are provided
  243.      only for backward compatibility.  The effect of padding the %%pp format
  244.      with zeros (either by the `00' flag or by specifying a precision), and the
  245.      benign effect (i.e., none) of the `##' flag on %%nn and %%pp conversions, as
  246.      well as other nonsensical combinations such as %%LLdd, are not standard;
  247.      such combinations should be avoided.
  248.  
  249.      Because sspprriinnttff() and vvsspprriinnttff() assume an infinitely long string,
  250.      callers must be careful not to overflow the actual space; this is often
  251.      impossible to assure.  For safety, programmers should use the ssnnpprriinnttff()
  252.      interface instead.  Unfortunately, this interface is not portable.
  253.  
  254. BSD Experimental                 July 30, 1991                               4
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.