Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <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 and prints a series of characters and values to
    'stdout', the standard output stream.

    '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.

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

    Returns:    The number of characters printed.  In the event of an
                error, EOF is returned.

      Notes:    If there are more arguments than there are format
                specifications, the extra arguments are ignored.  The
                results are undefined if there are not enough arguments
                for all the format specifications. Ordinary characters
                are simply copied in the order of their appearance.

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

 Escape sequences:  Escape sequences are special character combinations
                    that can represent whitespace and non-graphic
                    characters.  They are used to specify actions such as
                    carriage returns and tab movements. Escape sequences
                    consist of a backslash ('\') followed by a character
                    or combination of characters:

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

                    *    \ddd  byte value in octal notation
                    *    \xdd  byte value in hexadecimal notation
                    *    where 'd' represents a muneric digit.

                    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 (%) and have the following form:

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

                    Each field of the format specification is a single
                    character or number signifying a particular format
                    option.  The following describes each field.

          Type:     The 'type' character determines whether the
                    associated              argument is interpreted as a
                    character, string, pointer, or number.  The simplest
                    format specification contains only a percent sign and
                    a 'type' character. (For example: %s prints a
                    string.) 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 having 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 having the form
                                   [-]d.dddde[sign]ddd
                             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 upper-case "e" identifies the exponent.
                g  Floating-point.  Signed value printed in "f" or "e"
                                    format, whichever is more compact for the
                                    given value and precision, except that
                                    trailing zeros and decimal points are
                                    printed only if necessary. (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 Pointer   Characters are printed up to the first
                                    null character ('\0') or until'precision'
                                    is reached.
                n  Pointer to Int.  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, where
                                    xxxx is the segment, and yyyy is the
                                    offset. Digits 'x' and 'y' are uppercase
                                    hexadecimal digits; %Np prints
                                    only the offset of the address, yyyy.
                                    Since %p expects a pointer to a far
                                    value, pointer arguments to p must be
                                    cast to 'far' in small-model programs.

         Flags:     An optional sequence of flag characters controls the
                    justification of output 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, with padding on      with padding on
                      the right.                            the left.

           +          Prefix the output value with a        Sign appears
                      + or - sign, if it is of a signed     only 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, and a positive signed value
                      will be output with a sign.

           #          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        trailing zero
                      value to contain a decimal point      are 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:     The optional width specifier is a non-negative
                    decimal integer specifying the minimum number of
                    characters to print, padding with blanks and zeros.
                    Width never causes a value to be truncated.  The
                    width specification can be an asterisk (*). If so, an
                    argument from the argument list supplies the value;
                    the 'width' argument precedes the value being
                    formatted in the argument list.

      Precision     An optional precision specifier is a non-negative
                    decimal integer preceded by a period (.), which
                    specifies the maximum number of characters printed
                    for all or part of the output field, 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     With any of these types, precision specifies the
                    minimum number of digits to be printed. If number of
                    digits is less than specified in precision, output is
                    padded on the left with zeros.  The value is not
                    truncated when the number of digits exceeds
                    precision. By default, if precision is 0 or omitted,
                    the precision is set to 1.

          e,E,f     Precision specifies the number of digits to be
                    printed after the decimal point.  The last printed
                    digit is rounded. Default precision is 6.  If
                    precision is 0 or the period appears without a number
                    following it, no decimal point is printed.

            g,G     Precision specifies the maximum number of significant
                    digits printed.  By default, all significant digits
                    are printed.

              c     Precision has no effect.  By default, one character
                    is printed.

              s     Precision specifies the maximum number of characters
                    to be printed. Characters in excess of precision are
                    not printed. By default, characters are printed until
                    a null character is encountered.

        F|N|h|l     'F' and 'N' are prefixes that allow the user to
                    override the default addressing conventions of the
                    memory model being used. 'F' should be used in the
                    small model to print a value that has been declared
                    'far', and 'N' should be used in the medium, large,
                    and huge models for 'near' values.  'F' and 'N'
                    should be used only with the 's', 'n' and 'p' type
                    characters, since they are relevant only with
                    arguments that pass a pointer.  'h' is used as a
                    prefix with integer types 'd,i,o,u,x,X' to specify
                    that the argument is a 'short int'. 'l' is used as a
                    prefix with 'e,E,f,g,G' to show that the argument is
                    'double', rather than 'float', and with types
                    'd,i,o,u,x,X' to specify that the argument is a 'Long
                    Int'.

   -------------------------------- 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() vsprintf()

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