The following strings are used by the ANSI functions defined in the stdio.h file:
The fprintf, printf, sprintf, and other character print functions use a format control string that consists of the following:
Ordinary characters, which are written exactly as they occur in the format string
Conversion specifiers, which cause argument values to be fetched and written as they are encountered during the processing of the format string
Each conversion specification begins with a % character or with the character sequence of %n$. This character sequence is used to reorder the arguments. For more information, see Beginning Character Sequence.
The beginning is followed, in sequence, by the following:
Zero or more format control flags, which can modify the meaning of the format directive. See Format Control Flags.
An optional field width, which specifies the minimum width of the field. See Field Width.
An optional precision specifier, which specifies the number of digits to print. See Precision Specifiers.
An optional length modifier, which specifies the size of the argument. See Length Modifiers.
A conversion specifier that indicates the type of conversion to be performed. This specifier is required. For a list, see Conversion Specifiers.
The following table describes the valid format control flags, all of which are optional.
The field width specifier is optional. If the converted value has fewer characters than the specified field width, it is padded with spaces by default on the left; it is padded on the right, if the left-adjustment control flag (-) is given to the field width. The field width takes the form of an asterisk (*) or a decimal integer.
The precision specifier is optional and controls the number of digits printed for the following conversion specifiers:
For the b, B, d, i, o, u, x, and X (integer) conversion specifiers, indicates the minimum number of digits to appear.
For the a, A, e, E, f, and F conversions, indicates the number of digits to appear after the decimal-point character.
For the g and G conversion specifiers, indicates the maximum number of significant digits to appear.
For a string in the s conversion specifier, indicates the maximum number of characters to print.
The precision specifier takes the form of a period character (.) followed either by an asterisk character (*) or by an optional decimal digit string where a NULL digit string is treated as 0.
The following table describes the length modifiers, all of which are optional.
The following table describes the valid conversion specifiers.
The arguments must correspond with the conversion type specifiers, left to right in the string; otherwise, indeterminate results occur.
In the beginning character sequence (%n$), the n is a number and it is used to order the arguments. For example:
y = 4 str = "April is month number" printf ("%2$d %1$s", str, y); Results: "April is month number 4"
The following example uses the following strings:
str1 = "throw" str2 = "mama" str3 = "a kiss" str4 = "from the train"
The ordering of these strings can produce the following sequences:
printf ("%1$s %2$s %3$s %4$s", str1, str2, str3, str4); Results: "throw mama a kiss from the train" printf ("%2$s %1$s %3$s %4$s", str1, str2, str3, str4); Results: "mama throw a kiss from the train" printf ("%1$s %2$s %4$s %3$s", str1, str2, str3, str4); Results: "throw mama from the train a kiss"
Arguments are numbered starting with one. The number of directives must match the number of arguments; otherwise, indeterminate results occur. If unaccessed arguments in the format string are interspersed with ones that are accessed, indeterminate results occur.
The fscanf, scanf, sscanf, and other character scan functions use format control strings to specify acceptable input data.
The format control string consists of zero or more format directives, which specify acceptable input file data. Subsequent arguments are pointers to various types of objects that are assigned values as the format string is processed.
A format directive is composed of one of the following:
One or more whitespace characters
An ordinary character, which is any character but a percent (%) character or a whitespace character.
A conversion specification
A conversion specification is a sequence of characters, which begins with a % character or with the character sequence %n$. This is followed, in sequence, by the following:
An optional assignment suppression indicator: the asterisk character (*)
An optional decimal integer that specifies the maximum field width
An optional length modifier that specifies the size of the receiving object (see Length Modifiers)
A conversion specifier character that specifies that type of conversion to apply (see Conversion Specifiers)
As each format directive in the format string is processed, the directive can successfully complete, fail because of a lack of input data, or fail because of a matching error as defined by the particular directive. If end-of-file is encountered on the input data before any characters that match the current directive have been processed (other than leading whitespace where permitted), the directive fails for lack of data. If end-of-file occurs after a matching character has been processed, the directive is completed (unless a matching error occurs), and the function returns without processing the next directive. If a directive fails because of an input character mismatch, the character is left unread in the input stream. Trailing whitespace characters, including newline characters, are not read unless matched by a directive. When a format directive fails, or the end of the format string is encountered, the scanning is completed and the function returns.
When one or more whitespace characters—space, horizontal tab (\t), vertical tab (\v), form feed (\f), carriage return (\r), newline or line feed (\n)—occur in the format string, input data up to the first nonwhitespace character is read, or until no more data remains. If no whitespace characters are found in the input data, the scanning is complete and the function returns.
The following table describes the length modifiers, all of which are optional.
The following table describes the valid conversion specifiers.
A conversion type specifier other than those listed above causes scanning to terminate and the function to return.
The line
scanf ("%s%*f%3hx%d", name, &hexnum, &decnum)
with input
some_string 34.555e-3 abc1234
copies some_string into the array name, skips 34.555e-3, assigns 0xabc to hexnum and 1234 to decnum. The return value is 3.
The line
char fmt[100]; strcpy (fmt, "%[abcdefghijklmnopqrstuvwxyz"); strcat (fmt,"[ABCDEFGHIJKLMNOPQRSTUVWZ]%*2s%[W\n]"); scanf (fmt, string1, string2)
with input
They may look alike, but they don’t perform alike.
assigns
"They may look alike"
to string1, skips the comma and the space, and assigns
" but they don’t perform alike.".
to string2. (The %*2s only matches the ","; the next blank terminates that field.)
The fdopen and fopen functions use the following strings to specify the open mode for the file.