...printf and ...scanf Format Specifiers

 

In ...printf  format strings, format specifiers have the following form:

 

% [flags] [width] [.prec] [F|N|h|l|L] type_char

 

In ...scanf format strings, format specifiers have the following form:

 

% [*] [width] [F|N] [h|l|L] type_char

 

Each format specifier begins with the percent character (%).   After the % come the following:

 

Component            Opt./Req.         What It Controls or Specifies

 

  [flags]              (Optional)            ...printf flag character(s).  Output justification, numeric

                                                signs, decimal points, trailing zeros, octal and hex prefixes.

 

            -            Left-justifies the result, pads on the right with blanks. If not given, right-justifies

                        result, pads on left with zeros or blanks. 

            +              Signed conversion results always begin with a plus (+) or minus (-) sign.   

            blank             If value is non-negative, the output begins with a blank instead of a plus;

                        negative values still begin with a minus.  Plus (+) takes precedence over blank

                        ( ) if both are given.

 

            #              Specifies that the argument is to be converted using an alternate form. Has the

                        following effect on the argument being converted:

 

                        c s d i u      Has no effect 

                        o or O              Prepends 0 to a non-zero argument 

                        x or X              Prepends 0x (or 0X) to argument 

                        e E f              The result always contains a decimal point even if no digits follow the

                                    point. Normally, a decimal point appears in these results only if a digit

                                    follows it. 

                        g G              Same as e and E; trailing zeros are not removed

 

   [*]                   (Optional)             ...scanf assignment-suppression character.  Suppresses

                                                assignment of the next input field. 

           

  [width]              (Optional)        Width specifier.  For printf, minimum number of characters to

                                                print, padding with blanks or zeros. For scanf, specifies

                                                maximum number of characters to read; fewer characters might

                                                be read if the ...scanf function encounters a whitespace or

                                                unconvertible character. 

 

            For printf, nonexistent or small field widths do NOT cause truncation of a field. If the

            result of a conversion is wider than the field width, the field is expanded to contain the

            conversion result.

 

            n              At least n characters are printed. If the output value has less than n characters,

                        the output is padded with blanks (right-padded if "-" flag  is given, left-padded

                        otherwise).

            0n             At least n characters are printed. If the output value has less than n characters, it                 is filled on the left with zeros. 

 

  [.prec]              (Optional)            ...printf precision specifier.  Maximum number of

                                                characters to print; for integers, minimum number of digits to

                                                print.  Always begins with a period (.) to separate it from any

                                                preceding width specifier.

 

            (none)            Precision set to default: 

                        =             1 for d,i,o,u,x,X types 

                        =             6 for e,E,f types 

                        =             All significant digits for g,G types 

                        =             Print to first null character for s types 

                        =             No effect on c types

 

            0              For d,i,o,u,x types, precision set to default;

                        for e,E,f types, no decimal point is printed. 

 

            .n              n characters or n decimal places are printed. If the output value has more than n

                        characters, the output might be truncated or rounded. (Whether this happens

                        depends on the type character.)

 

            No numeric characters will be output for a field (i.e., the field will be blank) if the

            following conditions are all met:

 

                        -            you specify an explicit precision of 0

                        -            the format specifier for the field is one of the integer formats (d, i, o, u,

                                    or x )

                        -            the value to be printed is 0

 

            Precision has an effect on the conversion of the type_char specifier, as follows:

 

            Type

            Char             Effect of [.prec] (.n) on Conversion

 

            d            Specifies that at least n digits are printed.

            o            If input argument has less than n

            u            digits, output value is left-padded with zeros.

            X            If input argument has more than n digits, the output value is not truncated.

            e            Specifies that n characters are

            E            printed after the decimal point, and

            f            the last digit printed is rounded.

            g            Specifies that at most n significant

            G            digits are printed.

            c            Has no effect on the output.

            s            Specifies that no more than n characters are printed.

 

[F|N|h|l|L]             (Optional)        Input size modifier  Override default size of next input argument.

                                                For scanf, which expects address arguments, both the pointer

                                                size and target size can be modified.

                                                F             =             far pointer 

                                                N             =             near pointer 

                                                h             =             short int 

                                                l             =             long int (or double, For scanf)

                                                L             =             long double 

 

type_char              (Required)            Conversion type character

 

The information in this table is based on the assumption that no flag characters, width specifiers,

precision specifiers, or input-size modifiers were included in the format specifier.

 

Type

Char            Expected Arg    Format of output (printf)

 

Numerics (scanf expects address of int or float)

 

d          Integer                Signed decimal integer 

i          Integer              Signed decimal integer 

o          Integer              Unsigned octal integer 

u          Integer              Unsigned decimal integer 

x          Integer              Unsigned hexadecimal int (with a, b, c, d, e, f) 

X          Integer              Unsigned hexadecimal int (with A, B, C, D, E, F) 

f          Floating point     Signed value of the form [-]dddd.dddd. 

e          Floating point     Signed value of the form [-]d.dddd...e[+/-]ddd 

g          Floating point     Signed value in either e or f form, based on given value and

                                    precision. Trailing zeros and  the decimal point are printed if

                                    necessary. 

E          Floating point     Same as e; with E for exponent. 

G          Floating point     Same as g; with E for exponent if e format used

 

n          Integer              (scanf only) Stores number of characters scanned into

                                    integer at address of argument.

 

Characters (scanf expects address of arrays of char)

 

c            Character         Single character 

s          String pointer              Prints characters until a null-terminator is  found or

                                    precision is reached 

%            None               Prints the % character

 

Pointers

 

n          Pointer to int     Stores (in the location pointed to by the input  argument) a

                                    count of the chars written so far. 

p          Pointer              Prints the input argument as a pointer; format  depends on

                                    which memory model was used. It will  be either XXXX:YYYY

                                    or YYYY (offset only).

 

Infinite floating-point numbers are printed as +INF and -INF. 

 

An IEEE Not-A-Number is printed as +NAN or -NAN.

 

 

Search Set Conversion (%[search_set])

 

The set of characters surrounded by brackets can be substituted for the s-type character.

 

The address argument is a pointer to an array of characters (char arg[]).

 

These brackets surround a set of characters that define a search set of possible characters making up the string (the input field).

 

If the first character in the brackets is a caret (^), the search set is inverted to include all ASCII

characters except those between the brackets.

 

(Normally, a caret will be included in the inverted search set unless explicitly listed somewhere after the first caret.)

 

The input field is a string not delimited by whitespace. ...scanf reads the corresponding input field up to the first character it reaches that does not appear in the search set (or in the inverted search set).

 

Examples

 

%[abcd]              Searches the input field for any  of the characters a, b, c, and d 

%[^abcd]              Searches the input field for any  characters except a, b, c, and d

 

You can also use a range facility shortcut [<first>-<last>] to define a range of letters or numerals

in the search set.

 

Examples

 

To catch all decimal digits, you could define the search set with the explicit search set: 

%[0123456789] or with the range shortcut:  %[0-9]

 

To catch alphanumeric characters, you could use the following shortcuts:

 

%[A-Z]                        Catches all uppercase letters

%[0-9A-Za-z]            Catches all decimal digits and all letters 

%[A-FT-Z]                  Catches all uppercase letters from A through F and from T through Z.

 

Rules covering search set ranges

 

1.         The character prior to the hyphen (-) must be lexically less than the one after it.

 

2.         The hyphen must not be the first or last character in the set. (If it is first or last, it is considered to just be the hyphen character, not a range definer.)

 

3.         The characters on either side of the hyphen must be the ends of the range and not part of some other range.

 

 

When ...scanf Functions Stop Scanning

 

A ...scanf function might stop scanning a particular input field before reaching the normal field-end character (whitespace), or it might terminate entirely.

 

Stop and Skip to Next Input Field

 

...scanf functions stop scanning and storing the current input field and proceed to the next one if any of the following occurs:

 

-           An assignment-suppression character (*) appears after the % in the format specifier. The       current input field is scanned but not stored.

-           width characters have been read.

-           The next character read can't be converted under the current format (for example, an A

            when the format is decimal).

-           The next character in the input field does not appear in the search set (or does appear in

            an inverted search set).

 

When scanf stops scanning the current input field for one of these reasons, it assumes that the next character is unread and is either 

 

-           the first character of the following input field, or 

-           the first character in a subsequent read operation on the input.

 

Terminate

 

...scanf functions will terminate under the following circumstances:

 

1.         The next character in the input field conflicts with a corresponding non-whitespace

            character in the format string.

2.         The next character in the input field is EOF.

3.         The format string has been exhausted.

 

 

If a character sequence that is not part of a format specifier occurs in the format string, it must match the current sequence of characters in the input field.

 

...scanf functions will scan but not store the matched characters.

 

When a conflicting character occurs, it remains in the input field as if the ...scanf function never read it.