Format Specifier Conventions

From RAD Studio
Jump to: navigation, search

Go Up to scanf, wscanf

Certain conventions accompany some of the scanf format specifiers for the following conversions:

Format Specifier Conventions

single character (%c)

This specification reads the next character, including a whitespace character.

To skip one whitespace character and read the next non-whitespace character, use %1s.

character array (%[W]c)

[W] = width specification

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

string (%s)

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

The array size must be at least (n+1) bytes, where n = the length of string s (in characters).

A space or newline character terminates the input field.

A null terminator is automatically appended to the string and stored as the last element in the array.

floating-point (%e, %E, %f, %g, and %G)

Floating-point numbers in the input field must conform to the following generic format:

[+/-] ddddddddd [.] dddd [E|e] [+/-] ddd

where [item] indicates that item is optional, and ddd represents digits (decimal, octal, or hexadecimal).

In addition, +INF, -INF, +NAN, and -NAN are recognized as floating-point numbers. The sign (+ or -) and capitalization are required.

unsigned (%d, %i, %o, %x, %D, %I, %O, %X, %c, %n)

A pointer to unsigned character, unsigned integer, or unsigned long can be used in any conversion where a pointer to a character, integer, or long is allowed.

search sets(%[...], %[^...])

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

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.

See Also