Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C Library Reference - <u>synopsis:</u> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <stdio.h>
    int printf( const char *format, ... );
    #include <wchar.h>
    int wprintf( const wchar_t *format, ... );

Description:
    The printf function writes output to the file designated by  stdout
    under control of the argument format.  The format string is described
    below.

    The wprintf function is identical to printf except that it accepts a
    wide-character string argument for format.

Returns:
    The printf function returns the number of characters written, or a
    negative value if an output error occurred.  When an error has occurred,
     errno contains a value indicating the type of error that has been
    detected.

Example:
    #include <stdio.h>

    void main()
      {
        char *weekday, *month;

        weekday = "Saturday";
        month = "April";
        printf( "%s, %s %d, %d\n",
              weekday, month, 18, 1987 );
        printf( "f1 = %8.4f f2 = %10.2E x = %#08x i = %d\n",
                23.45,      3141.5926,   0x1db,     -1 );
      }

    produces the following:

    Saturday, April 18, 1987
    f1 =  23.4500 f2 =  3.14E+003 x = 0x0001db i = -1

Format Control String:
    The format control string consists of ordinary characters, that are
    written exactly as they occur in the format string, and conversion
    specifiers, that cause argument values to be written as they are
    encountered during the processing of the format string.  An ordinary
    character in the format string is any character, other than a percent
    character (%), that is not part of a conversion specifier.  A conversion
    specifier is a sequence of characters in the format string that begins
    with a percent character (%) and is followed, in sequence, by the
    following:

     .  zero or more format control flags that can modify the final effect
        of the format directive;
     .  an optional decimal integer, or an asterisk character ('*'), that
        specifies a minimum field width to be reserved for the formatted
        item;
     .  an optional precision specification in the form of a period
        character (.), followed by an optional decimal integer or an
        asterisk character (*);
     .  an optional type length specification:  one of "h", "l", "L", "I64",
        "w", "N" or "F"; and
     .  a character that specifies the type of conversion to be performed:
         one of the characters "cCdeEfFgGinopsSuxX".

    The valid format control flags are:

    "-"
        the formatted item is left-justified within the field; normally,
        items are right-justified

    "+"
        a signed, positive object will always start with a plus character
        (+); normally, only negative items begin with a sign

    " "
        a signed, positive object will always start with a space character;
        if both "+" and " " are specified, "+" overrides " "

    "#"
        an alternate conversion form is used:

         .  for "o" (unsigned octal) conversions, the precision is
            incremented, if necessary, so that the first digit is "0".
         .  for "x" or "X" (unsigned hexadecimal) conversions, a non-zero
            value is prepended with "0x" or "0X" respectively.
         .  for "e", "E", "f", "g" or "G" (any floating-point) conversions,
            the result always contains a decimal-point character, even if no
            digits follow it; normally, a decimal-point character appears in
            the result only if there is a digit to follow it.
         .  in addition to the preceding, for "g" or "G" conversions,
            trailing zeros are not removed from the result.


    If no field width is specified, or if the value that is given is less
    than the number of characters in the converted value (subject to any
    precision value), a field of sufficient width to contain the converted
    value is used.  If the converted value has fewer characters than are
    specified by the field width, the value is padded on the left (or right,
    subject to the left-justification flag) with spaces or zero characters
    ("0").  If the field width begins with a zero, the value is padded with
    zeros, otherwise the value is padded with spaces.  If the field width is
    "*", a value of type int from the argument list is used (before a
    precision argument or a conversion argument) as the minimum field width.
     A negative field width value is interpreted as a left-justification
    flag, followed by a positive field width.

    As with the field width specifier, a precision specifier of "*" causes a
    value of type int from the argument list to be used as the precision
    specifier.  If no precision value is given, a precision of 0 is used.
     The precision value affects the following conversions:

     .  for "d", "i", "o", "u", "x" and "X" (integer) conversions, the
        precision specifies the minimum number of digits to appear
     .  for "e", "E" and "f" (fixed-precision, floating-point) conversions,
        the precision specifies the number of digits to appear after the
        decimal-point character
     .  for "g" and "G" (variable-precision, floating-point) conversions,
        the precision specifies the maximum number of significant digits to
        appear
     .  for "s" or "S" (string) conversions, the precision specifies the
        maximum number of characters to appear

    A type length specifier affects the conversion as follows:

     .  "h" causes a "d", "i", "o", "u", "x" or "X" (integer) format
        conversion to treat the argument as a short int or unsigned short
        int argument.  Note that, although the argument may have been
        promoted to an int as part of the function call, the value is
        converted to the smaller type before it is formatted.
     .  "h" causes an "f" format conversion to interpret a long argument as
        a fixed-point number consisting of a 16-bit signed integer part and
        a 16-bit unsigned fractional part.  The integer part is in the high
        16 bits and the fractional part is in the low 16 bits.


             struct fixpt {
                 unsigned short fraction; /* Intel architecture! */
                   signed short integral;
             };

             struct fixpt foo1 =
               { 0x8000, 1234 }; /* represents 1234.5 */
             struct fixpt foo2 =
               { 0x8000, -1 };   /* represents -0.5 (-1+.5) */

        The value is formatted with the same rules as for floating-point
        values.  This is a Watcom extension.
     .  "h" causes an "n" (converted length assignment) operation to assign
        the converted length to an object of type unsigned short int.
     .  "h" causes an "s" operation to treat the argument string as an ASCII
        character string composed of 8-bit characters.

        For  printf and related byte input/output functions, this specifier
        is redundant.  For  wprintf and related wide character input/output
        functions, this specifier is required if the argument string is to
        be treated as an 8-bit ASCII character string; otherwise it will be
        treated as a wide character string.


             printf(    "%s%d", "Num=", 12345 );
             wprintf( L"%hs%d", "Num=", 12345 );
     .  "l" causes a "d", "i", "o", "u", "x" or "X" (integer) conversion to
        process a long int or unsigned long int argument.
     .  "l" causes an "n" (converted length assignment) operation to assign
        the converted length to an object of type unsigned long int.
     .  "l" or "w" cause an "s" operation to treat the argument string as a
        wide character string (a string composed of characters of type
         wchar_t).

        For  printf and related byte input/output functions, this specifier
        is required if the argument string is to be treated as a wide
        character string; otherwise it will be treated as an 8-bit ASCII
        character string.  For  wprintf and related wide character
        input/output functions, this specifier is redundant.


             printf(  "%ls%d", L"Num=", 12345 );
             wprintf( L"%s%d", L"Num=", 12345 );
     .  "F" causes the pointer associated with "n", "p", "s" conversions to
        be treated as a far pointer.
     .  "L" causes a "d", "i", "o", "u", "x" or "X" (integer) conversion to
        process an __int64 or unsigned __int64 argument (e.g., %Ld).
     .  "I64" causes a "d", "i", "o", "u", "x" or "X" (integer) conversion
        to process an __int64 or unsigned __int64 argument (e.g., %I64d).
         The "L" specifier provides the same functionality.
     .  "L" causes an "e", "E", "f", "g", "G" (double) conversion to process
        a long double argument.
     .  "N" causes the pointer associated with "n", "p", "s" conversions to
        be treated as a near pointer.

    The valid conversion type specifiers are:

    c
        An argument of type int is converted to a value of type char and the
        corresponding ASCII character code is written to the output stream.

    C
        An argument of type wchar_t is converted to a multibyte character
        and written to the output stream.

    d, i
        An argument of type int is converted to a signed decimal notation
        and written to the output stream.  The default precision is 1, but
        if more digits are required, leading zeros are added.

    e, E
        An argument of type double is converted to a decimal notation in the
        form [-]d.ddde[+|-]ddd similar to FORTRAN exponential (E) notation.
        The leading sign appears (subject to the format control flags) only

See Also:
    _bprintf, cprintf, fprintf, sprintf, _vbprintf, vcprintf, vfprintf,
    vprintf, vsprintf

See Also: cprintf vcprintf

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson