Input Format Strings

When using certain functions for input, such as scanf, one requires an input format string specifying the types of variables which data will go into, along with any extra information the user must type to format the data.

The format specifiers that go into these format strings are very similar to those that are used for output format strings. A complete input format specifier looks as follows-na:

% * width size type

Of these fields, only the % and type are compulsory.

The size and type fields are used in almost exactly the same way as for output format strings (see the tables above). This time, of course, they specify the type and size of the incoming data.

Two of the type specifiers are different for input however:

c specifies that the number of characters specified by the width field should be read. If the width field is missing, it is taken to be 1. Of course, to store a number of characters, one must use an array rather than a simple variable of type char. However, recall that with functions like scanf one must supply pointers to the variables used by appending ampersands to their names or by giving the name of an array. Thus the parameter requirements for functions like scanf are already in effect demanding that an array be used. One peculiarity with this format specifier is that no end of string, "\0", character is added. Also, any whitespace which is input is also stored in the array, which is not the same as with the s format specifier which only considers non-whitespace characters to be input which is to be recorded in the corresponding array.

[ specifies that the input must only use characters from the set of characters following it (and before a closing ] to delineate the end of the set). The parameter used must again be an array of char's.

[^ specifies that the input must only use characters not in the set of characters following it (and before the closing square bracket).

In order to include a ] in the set of allowed/disallowed characters, just add a ] immediately after the opening square bracket followed by the other allowed/disallowed characters then the usual closing ].

Here are some examples of input strings illustrating these differences with format strings for output:

    char buffer1[10];
    scanf("[][123,]",buffer1);  //allows-na characters "[", "]", "1", "2", "3" and ","

The width field, when it is used, specifies the maximum number of characters which the user will enter for the corresponding variable or array.

The asterisk is used to specify that the variable corresponding to the current part of the format string should not actually be filled with the data which the user enters. This is basically used to ignore data of a specified type, coming from the user. This is useful when used in combination with the type specifiers [ and [^.