Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ printf(3C) — DG/UX 4.00

Media Vault

Software Library

Restoration Projects

Artifacts Sought



                                                               printf(3C)



        _________________________________________________________________
        printf                                                   function
        Perform a formatted write to standard output.
        _________________________________________________________________


        Calling Sequence

        #include <stdio.h>
        int printf();
        char *format; ...
        printf(format, arg1 [,arg2 ...]);


          where   each arg is a valid C variable or constant.
                  format is a string containing two kinds of objects:

                  * Ordinary ASCII characters that the function copies
                  directly.

                  * Conversion specifications that start with %.




        Description

        The printf function writes to the standard output file.  It
        replaces format specifications with successive arguments and
        writes the results to the file.

        After the % character come specifications for precision, signs,
        and the like.  They are optional, but you must enter them in the
        following relative order:

        1)   Zero or more flag characters:

        $    Add a floating dollar sign before d, i, e, E, f, g, and G
             formats (a Data General feature).

        --   Specify left adjustment instead of right.

        +    Specify that d, i, e, E, f, g, and G formats will have a
             sign, even if it is positive.

        (blank)
             Specify that if no sign is produced, a space prefixes the
             field. (If you use + with the blank, the blank is ignored.)

        #    Specify an alternate format: %o adds a preceding 0 (for
             octal values), %x and %X add a preceding 0x and 0X



        DG/UX 4.00                                                 Page 1
               Licensed material--property of copyright holder(s)





                                                               printf(3C)



             respectively (for hexadecimal), and in floating conversions
             the decimal point always appears, whether or not there are
             trailing digits.

        2)   A digit string, specifying the minimum field width.  If the
             first character is 0, it specifies padding with zeros
             instead of spaces.  If the first character is * or @, printf
             will take the field width from the next integer argument.
             (The @ is a Data General feature.)

        3)   A precision field (a period followed by a digit string),
             followed by an optional decimal integer.  This precision
             field controls the number of digits to be printed for a
             numeric conversion.

             For integral and pointer formats (d, i, o, p, u, x, and X),
             if the precision field is not supplied, 1 is assumed.
             Leading 0's are used to pad the field to the number of
             characters specified by the precision field.  If the
             precision field is 0 and the number is 0, nothing is
             printed.

             See the e, E, f, g, and G conversion characters for details
             on floating-point formats.

             If * or @ occurs here instead of the digit string, printf
             will take the field width from the next integer argument.
             (The @ is a Data General feature.)

        4)   An l or L, indicating that the corresponding argument is of
             type long rather than type int; or an h or H, which
             indicates that the item is of type short, and specifies
             truncating the number before printing.

        5)   A conversion character:

        d    Print an integral argument in signed decimal.

        i    Print an integral argument in signed decimal.

        o    Print an integral argument in octal.

        x    Print an integral argument in hexadecimal, with digits
             greater than 9 in lowercase (a - f).

        X    Print an integral argument in hexadecimal, with digits
             greater than 9 in uppercase (A - F).

        u    Print an integral argument as unsigned.

        c    Print a single character.



        DG/UX 4.00                                                 Page 2
               Licensed material--property of copyright holder(s)





                                                               printf(3C)



        s    Print a string ending in a null or having up to the
             specified maximum precision characters.

        t    Specify tabs, using the format options %@t, %*t, and %nt,
             where n is a column number.  The %nt option specifies to
             jump to column n and begin printing there; you always
             generate at least one blank.  The %@t or %*t option
             indicates to skip to the column specified by the value of
             the function's next argument.  The printf function assumes
             that the file is positioned at column 1 when called; New
             Lines that are printed get reset.  The t conversion
             character is a Data General feature.

        e    Print a double argument in scientific notation, using e as
             the exponent.  The precision field (field after the period)
             specifies the number of digits to print after the decimal
             point.

        E    Print a double argument in scientific notation, using E
             instead of e for the exponent.  The precision field (field
             after the period) specifies the number of digits to print
             after the decimal point.

        f    Print a double argument in normal notation.  The precision
             field (field after the period) specifies the number of
             digits to print after the decimal point.

        g    Print a double argument in normal or scientific notation,
             whichever is shorter.  The precision field (field after the
             period) specifies the number of significant digits to print
             out, instead of the number of digits to be printed after the
             decimal point as the field did before rev 4.00.

        G    Print a double argument in normal or scientific notation,
             whichever is shorter, and use E instead of e for the
             exponent.  The precision field (field after the period)
             specifies the number of significant digits to print out,
             instead of the number of digits to be printed after the
             decimal point as the field did before rev 4.00.

        n    Assume that the next argument is a pointer to an integer in
             which the number of characters written so far will be
             stored.  This does not print anything directly.

        r    Use the string argument as a nested format (a Data General
             feature).  This does not print anything directly.

        p    Print the byte pointer argument as word pointer + offset (a
             Data General feature).

        %    Print a single percent sign.  Since percent signs indicate



        DG/UX 4.00                                                 Page 3
               Licensed material--property of copyright holder(s)





                                                               printf(3C)



             the beginning of a conversion specification, you must write
             two of them to have one printed.  See the "Input/Output
             Functions and Macros" section of Chapter 1 for information
             on buffering with the I/O functions and macros.


        Returns

        The printf function returns -1 if an error occurs; otherwise it
        returns the number of characters printed.


        Related Functions

        See also the fprintf, scanf, sprintf, and vprintf functions.


        Examples

        1.      /* Program test for the printf() function on integers */

        #include <stdio.h>

        int  n, i = 1;

        main(argc, argv)
        int     argc;
        char   *argv[];
        {
            printf("Here are some ways to print out integers:\n\n");
            while (i < argc) {
                n = atoi(argv[i]);
                printf("%07d\t%$d\t%-d\t%+d\t%#o\t%#x\n",
                        n, n, n, n, n, n);
                i++;
            }
        }

        A call to the program test with the numbers 123, -45, and 6789
        generates the output

        Here are some ways to print out integers:


        0000123   $123    123    +123    0173           0x7b
        -000045   $-45    -45    -45     037777777723   0xffffffd3
        0006789   $6789   6789   +6789   015205         0x1a85

        2.      /* Program test for the printf() function on floats */

        #include <stdio.h>



        DG/UX 4.00                                                 Page 4
               Licensed material--property of copyright holder(s)





                                                               printf(3C)



        double  n, atof();
        int     i = 1;

        main(argc, argv)
        int     argc;
        char   *argv[];
        {
            printf("Here are some ways to print out");
            printf(" floating-point numbers:\n\n");
            while (i < argc) {
                n = atof(argv[i]);
                printf("%04f\t%e\t%.3f\n%#o\t%#x\t%g\n\n",
                        n, n, n, n, n, n);
                i++;
            }
        }

        A call to program test with the numbers 0.12345, 12.345, and
        1234.5 generates the output

        Here are some ways to print out floating-point numbers:


          0.123450       1.234500e-01   0.123
          012054171174   0x401f9a6b     0.12345

          12.345000      1.234500e+01   12.345
          027024365605   0x41c5851e     12.345

          1234.500000    1.234500e+03   1234.500
          0              0x434d2800     1234.5

        3.      /* Program test for the printf() function on strings */

        #include <stdio.h>

        int  i = 1;

        main(argc, argv)
        int     argc;
        char    *argv[];
        {
            printf("Here are some ways to print out strings:\n\n");
            while (i < argc) {
                printf("%s\t", argv[i]);
                printf("%06s\t", argv[i]);
                printf("%.3s\n", argv[i]);
                printf("%5t%s\t", argv[i]);
                i++;
            }
        }



        DG/UX 4.00                                                 Page 5
               Licensed material--property of copyright holder(s)





                                                               printf(3C)



        A call to the program test with the string

        Look at the last letters:

        generates the output

        Here are some ways to print out strings:

        Look    00Look  Loo
            Look
        at      0000at  at
            at
        the     000the  the
            the
        last    00last  las
            last
        letters:        letters:      let
            letters:

        The second instance of each argument is extended by zeros to a
        length of 6.  The third instance is truncated to a length of 3,
        and the fourth is tabbed to column 5.
































        DG/UX 4.00                                                 Page 6
               Licensed material--property of copyright holder(s)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026