Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>printf() write formatted string to stdout</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 printf()                Write Formatted String to Stdout

 #include   <stdio.h>

 int        printf(format-string[,argument...]);
 const char *format-string;              Format control

    printf() formats a series of characters and values and prints them to
    'stdout'.

    'format-string' determines what is to be printed and how it is to be
    printed out. 'format-string' consists of ordinary characters, escape
    sequences, and format specifications, which are preceded by a percent
    sign.

    The 'format-string' is read left to right.  The first format
    specification is used to convert and output the value of the first
    argument after the 'format-string'.  The second format specification
    causes the second argument to be converted and output, and so on.

    Returns:    On success, the number of characters printed; on error, -
                1 is returned.

      Notes:    If the number of arguments exceeds the number of format
                specifications, the extra arguments are ignored.  If
                there are more format specification than arguments, the
                results are undefined. Ordinary characters are simply
                copied in the order of their appearance.

                If the character following the percent sign has no
                meaning as a format field, the character is copied to
                'stdout'.

  Escape sequences:
                Escape sequences are character combinations that begin
                with a backslash and are used to represent whitespace and
                non-graphic characters--for example, carriage returns and
                tab movements.  The following are recognized escape
                sequences:

                    \n  new-line       \t    horizontal tab
                    \b  backspace      \r    carriage return
                    \a  bell (alert)   \'    single quote
                    \\  backslash      \f    form-feed
                    \v  vertical tab   \"    double quotes

                    \ddd  byte value in octal notation
                    \xdd  byte value in hexadecimal notation

                If there are arguments following 'format-string', then
                'format-string' must contain format specifications that
                determine the output format for these arguments.  Format
                specifications always begin with a percent sign (%).  At
                its simplest, a format specification can be nothing more
                than a percent sign followed by a 'type' character; for
                example, %d would indicate a signed decimal integer.
                Format specifications have the following form:

                    %[flags][width][.precision][{F|N|h|l|L}]type

                Each field of the format specification is one character
                or number that indicates a particular format option.  The
                following describes each field.

       Type:    The 'type' character is included after all optional
                format-specification fields and indicates whether the
                associated argument is to be interpreted as a character,
                a string, or a number. The 'type' characters are:

                d   Integer         Signed decimal integer
                i   Integer         Signed decimal integer
                u   Integer         Unsigned decimal integer
                o   Integer         Unsigned octal integer
                x   Integer         Unsigned hexadecimal integer  (abcdef)
                X   Integer         Unsigned hexadecimal integer  (ABCDEF)

                f   Floating point  Signed value of the form:
                                [-]dddd.dddd
                         where dddd is one or more decimal digits. The number
                         of digits after the decimal point depends on the
                         requested precision.

                e   Floating-point.  Signed value of the form
                           [-]d.dddde[sign]ddd, where:
                         d     is a single decimal digit
                         dddd  is one or more decimal digits
                         ddd   is exactly three decimal digits,
                         sign  is + or -

                E  Floating-point.  Identical to the "e" format above, except
                that the exponent is introduced with a capital "E".

                g  Floating-point.  Signed value printed in "f" or "e"
                format, whichever is more compact for the given value and
                precision. (See precision chart)

                G  Floating-point.  Identical to "g" format, except that "E"
                introduces the exponent (where appropriate) instead of "e".

                c  Character        Single character

                s  String           Characters printed up to the first null
                character ('\0') or until 'precision' is reached.

                n  Pointer to       Number of characters written so far to
                the Integer stream or buffer; this value is stored in the
                integer whose address is given as the argument.

                p  Far pointer      Prints the address pointed to by the
                argument in the form xxxx:yyyy. xxxx is the segment, and yyyy
                is the offset. The x's and y's represent uppercase
                hexadecimal digits. %Np prints
                only the offset of the address, yyyy.
                Since %p expects a far pointer, pointer arguments to p must
                be cast to 'far' in small-model programs.

      Flags:    Optional.  A sequence of flag characters governs output
                justification and printing of signs, blanks, decimal
                points, and octal and hexadecimal prefixes.  The flag
                characters available are:

          Flag                Action                           Default
          ---------------------------------------------------------------
           -          Left justify the result within        Right justify
                      the field width.

           +          Prefix the output value with a        Sign appears only
                      + or - sign if it is of a signed      for negative
                      type.                                 signed values(-)

           blank      Prefix the output value with a        No blank
                      blank (ASCII 32) if the output
                      value is signed and positive;
                      the "+" flag overrides the blank
                      flag.

           #          When used with o, x, or X, the        No prefix
                      "#" flag prefixes any nonzero
                      output value with 0, 0x, or 0X,
                      respectively.

                      When used with e, E, or f format,     Decimal point
                      the "#" flag forces the output        appears only if
                      value to contain a decimal point      digits follow it.
                      in all cases.

                      When used with g or G format,         As above, and
                      the "#" flag forces the output        zeros are
                      value to contain a decimal point      truncated.
                      in all cases, and prevents the
                      truncation of trailing zeros.

                      Ignored when used with c, d, i, u,
                      or s.

                More than one flag can appear in a format specification.

      Width:    Optional; a non-negative decimal integer.   Width
                specifies the minimum number of characters to be printed;
                if the number of characters is less than width, the
                output can be padded with blanks and zeros.  Because
                width specifies the minimum (not maximum) number of
                characters, values are never truncated.  If you specify
                width as an asterisk (*), an argument from the argument
                list is used to supply the value; in this case, the
                'width' argument must appear before the value to be
                formatted.

  Precision:    Optional; a period (.) followed by a non-negative decimal
                integer.  Precision specifies the maximum number of
                characters printed for all or part of the output field,
                the number of decimal places printed, or the minimum
                number of digits printed for integer values.  Precision
                can cause truncation of the output value, or rounding in
                the case of a floating-point value.  Here's how precision
                can affect different types:

 d,i,o,u,x,X:   Determines the minimum number of digits printed. If there
                are fewer digits than specified in precision, zeros are
                used on the left to pad the output.  If there are more
                digits than specified in precision, the value is not
                truncated. By default, if precision is 0 or omitted, the
                precision is set to 1.

      e,E,f:    Specifies the number of digits to follow the decimal
                point and rounds the last printed digit. Default
                precision is 6.  No decimal point is printed if precision
                is 0 or if a number does not follow the period.

        g,G:    Specifies the maximum number of significant digits
                printed.  By default, all significant digits are printed.

          c:    Has no effect.  By default, one character is printed.

          s:    Specifies the maximum number of characters to be printed;
                does not print any characters in excess of precision. By
                default, characters are printed until a null character is
                encountered.

 F|N|h|l|L: Optional.  'F' and 'N' are prefixes that can be used to
                override the default addressing conventions of the memory
                model used.  Using 'F' in the small model prints a value
                declared as 'far'; using 'N' in the medium, large, and
                huge models prints 'near' values.  'F' and 'N' are
                appropriate only if an argument passes a pointer and so
                should be used only with the 's' and 'p' type characters.
                'h' and 'l' are prefixes that indicate argument size.
                Used as a prefix with integer types 'd,i,o,u,x,X', 'h'
                specifies a 'short int', 'l' specifies a 'long int'.  If
                used as a prefix with 'e,E,f,g,G', 'l' also shows that
                the argument is 'double', rather than 'float'.  'L' is
                used as a prefix with 'e,E,f,g,G' to show that the
                argument is a long double.

 Portability:   The "F" and "N" prefixes are Microsoft extensions and
                should not be used where ANSI portability is required.

   -------------------------------- Example ---------------------------------

    The following statements format and print various data.

           #include <stdio.h>

           int val = 5559, x;
           double lval = 1538.672;
           char ch = 'a';
           char *string1 = "how to ";
           char *string2 = "format and print ";
           char *string3 = "data ";
           char pause;

           main()
           {
               printf("Here's %saccurately %svarious %s.\n",
                      string1,string2,string3);
               printf("Press any key to continue...");
               pause = getchar();
               printf("\n\t   ASCII Characters and Decimal
                      Equivalents\n\n");
               for (x = 0; x < 13; x++)
                   printf("\t\t%d\t%c\t\t%d\t%c\n",x+65,x+65,x+78,x+78);
               printf("Press any key to continue...");
               pause = getchar();
               printf("\n\ninteger value: %d\n\n",val*2);
               printf("floating point values: %.2f   %e \n\n",lval,lval);
               for (x = 0; x <100; x++)
                   printf("%c\t",ch);
           }


See Also: fprintf() scanf() sprintf() vfprintf() vprintf()

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