20.1 ANSI String Values

The following strings are used by the ANSI functions defined in the stdio.h file:

20.1.1 Print Format Control Strings

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.

Format Control Flags

The following table describes the valid format control flags, all of which are optional.

' <single quote>

The integer portion of the result of a decimal conversion (%i, %d, %u, %f, %F, %g, or %G) is formatted with the thousands' grouping character. The non-monetary grouping character is used.

- <hyphen>

The result of the conversion is left-justified within the field. The results are right-justified when this flag is not specified.

+

The result of a signed conversion is always prepended with a sign (+ or -). If this flag is not specified, the conversion begins with a sign only when a negative value is converted.

<space>

If the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a <space> is prefixed to the result. If the <space> and + flag both appear, the <space> flag is ignored.

#

An alternate conversion form is used:

  • For o (unsigned octal) conversion specifier, the precision is incremented so that the first digit is zero.

  • For x of X (unsigned hexadecimal) conversion specifier, a nonzero value is prepended with 0x or 0X, respectively.

  • For b or B (binary) conversion specifier, the value is prepended with 0b or 0B, respectively.

  • For a, A, e, E, f, F, g, or G (any floating point) conversion specifier, the result always contains a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result only if there is a digit to follow it. For g and G conversion specifiers, trailing zeros are not removed from the result.

0

For d, i, o, u, x, X, a, A, e, E, f, F, g, or G conversion specifier, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed.

If the 0 and - flags both appear, the 0 flag is ignored.

For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag is ignored.

If the 0 and ' flags both appear, the grouping characters are inserted before zero padding.

Field Width

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.

Precision Specifiers

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.

Length Modifiers

The following table describes the length modifiers, all of which are optional.

h

For the b, B, d, i, o, u, x, or X conversion specifier, applies a short or unsigned short to the argument.

For an n (converted length assignment) operation, assigns the converted length to an object of type short.

hh

For the b, B, d, i, o, u, x, or X conversion specifier, treats the argument as a signed char or unsigned char.

For an n (converted length assignment) operation, assigns the converted length to an object of type signed char.

j

For the b, B, d, i, o, u, x, or X conversion specifier, applies a intmax_t or uintmax_t to the argument.

For an n (converted length assignment) operation, assigns the converted length to an object of type intmax_t.

l (ell)

For the b, B, d, i, o, u, x, or X conversion specifier, applies a long int or unsigned long int to the argument.

For an n (converted length assignment) operation, assigns the converted length to an object of type unsigned long int.

ll (ell-ell)

For the b, B, d, i, o, u, x, or X conversion specifier, applies a long long or unsigned long long to the argument.

For an n (converted length assignment) operation, assigns the converted length to an object of type unsigned long long.

L

For the a, A, e, E, f, F, g, or G conversion specifier, applies a long double to the argument.

t

For the b, B, d, i, o, u, x, or X conversion specifier, applies a ptrdiff_t or the corresponding unsigned type to the argument.

For an n (converted length assignment) operation, assigns the converted length to an object of type ptrdiff_t

z

For the b, B, d, i, o, u, x, or X conversion specifier, applies a size_t or the corresponding signed integer type to the argument.

For an n (converted length assignment) operation, assigns the converted length to an object of type size_t.

Conversion Specifiers

The following table describes the valid conversion specifiers.

a, A

An argument of type double is converted to a floating point number in the form [-]0xh.hhhhd, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character and the number of hexadecimal digits after it is equal to the precision.

  • If the precision is missing and FLT_RADIX is a power of 2, the precision is made sufficient for an exact representation of the value.

  • If the precision is missing and FLT_RADIX is not a power of 2, the precision is made sufficient to distinguish values of type double, except that trailing zeros are omitted.

  • If the precision is zero and the # flag is not specified, no decimal-point character appears.

The letters “abcdef” are used for the “a” conversion specifier and the letters “ABCDEF” are used for the “A” conversion specifier. The A conversion specifier produces a number with “X” and “P” instead of “x” and “p”. The exponent always contains at least one digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the value is zero, the exponent is zero.

b, B

An argument of type int or unsigned is converted to binary.

c

An argument of type int is converted to a value of type unsigned char, and the resulting byte is written to the output stream.

If an l (ell) qualifier is present, the wint_t argument is converted to wchar_t and written.

C

Equivalent to the lc conversion specifier.

d, i

An argument of type int is converted to a signed decimal in the form [-]dddd and written to the output stream. The precision specifies the minimum number of digits. If the value can be represented in fewer digits, leading zeros are added.The default precision is 1.

e, E

An argument of type double is converted to a decimal notation in the form [-]d.ddde[]dd, where one digit appears before the decimal-point character and the number of digits after it is equal to the precision. The leading sign appears (subject to the format control flags) only if the argument is negative.

The precision is used to specify the number of digits following the decimal-point character. If the precision is not specified, a default precision of 6 is used. If the precision is zero and no # flag is present, the decimal-point character is suppressed and the value is rounded to the appropriate number.

For E conversions, the exponent begins with the character E rather than e. The exponent always contains at least a two-digit number.

A double argument representing an infinity or NaN is converted in the same manner as the f or F conversion specifier.

f, F

An argument of type double is converted to a decimal notation in the form [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specifier. The leading sign appears (subject to the format control flags) only if the argument is negative.

The precision is used to specify the number of digits following the decimal point character. If the precision is not specified, a default precision of 6 is used. If the precision is zero and no # flag is present, the decimal-point character is suppressed and the value is rounded. Otherwise, at least one digit is produced before the decimal-point character.

A double argument representing an infinity is converted to [-] infinity, and one representing a NaN is converted to [-]nan. The F conversion specifier converts the argument, respectively, to [-]INFINITY or [-]NAN.

g, G

An argument of type double is converted using either the f or e style of conversion depending on the value of the argument (F or E, for a G conversion). In either case, the precision specifies the number of significant digits that are contained in the result. The e style conversion is used only if the exponent from such a conversion would be less than -4 or greater than the precision. Trailing zeros are removed from the result, and a decimal-point character only appears if it is followed by a digit.

n

The number of characters that have been written to the output stream is assigned to the integer pointed to by the argument. No output is produced.

o

An argument of type unsigned int is converted to an unsigned octal notation in the form dddd and written to the output stream. The precision specifies the minimum number of digits. If the value can be represented in fewer digits, leading zeros are added. The default precision is 1.

p, P

An argument of type void * is converted to a value of type int and the value is formatted as for a hexadecimal x conversion for p and a hexadecimal X conversion for P.

s

If no l (ell) qualifier is present, the argument is a pointer to an array of char. The characters from the array are written, up to, but not including, the null-terminating character.

  • If the precision is specified, no more than that many characters are written.

  • If the precision is not specified, or is greater than the size of the array, a null-terminating character is written.

If an l (ell) qualifier is present, the argument is a pointer to an array of wchar_t. The wide characters from the array are written up to, but not including, a wide null-terminating character.

  • If the precision is specified, no more than that many wide characters are written.

  • If the precision is not specified, or is greater than the size of the array, a null-terminating wide character is written.

S

Nonstandard conversion. Characters from a length-preceded string are written to the output stream. If a precision is specified, no more than that many characters are written.

U

Equivalent to the ls conversion specifier.

u

An argument of type int is converted to an unsigned decimal notation and written to the output stream. The precision specifies the minimum number of digits. If the value can be represented in fewer digits, leading zeros are added. The default precision is 1.

x, X

An unsigned argument is converted to an unsigned hexadecimal notation and written to the output stream. The precision specifies the minimum number of digits. If the value can be represented in fewer digits, leading zeros are added. The default precision is 1.

Hexadecimal notation uses digits (0 through 9) and characters (a through f or A through F) for x or X conversions respectively. Subject to the alternate-form control flag, 0x or 0X is affixed to the output.

%

A % character is written to the output stream. No arguments are converted. The entire conversion specification is %%.

The arguments must correspond with the conversion type specifiers, left to right in the string; otherwise, indeterminate results occur.

Beginning Character Sequence

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.

20.1.2 Scan Format Control Strings

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.

Length Modifiers

The following table describes the length modifiers, all of which are optional.

h

For the d, i, o, u, x, X, or n conversion specifier, applies a short or unsigned short to the object pointed to by the argument.

hh

For the d, i, o, u, x, X, or n conversion specifier, applies a signed char or unsigned char to the object pointed to by the argument.

j

For the d, i, o, u, x, X, or n conversion specifier, applies an intmax_t or uintmax_t to the object pointed to by the argument.

l (ell)

For the d, i, o, u, x, X, or n conversion specifier, applies a long int or unsigned long int to the object pointed to by the argument.

For the a, A, e, E, f, F, g, or G conversion specifier, applies a double to the object pointed to by the argument.

For the c, s, or [ conversion specifier, applies a wchar_t to the object pointed to by the argument

ll (ell-ell)

For the d, i, o, u, x, X, or n conversion specifier, applies a long long or unsigned long long to the object pointed to by the argument

L

For the a, A, e, E, f, F, g, or G conversion specifier, applies a long double to the object pointed to by the argument.

t

For the d, i, o, u, x, X, or n conversion specifier, applies a ptrdiff_f or the corresponding unsigned type to the object pointed to by the argument.

z

For the d, i, o, u, x, X, or n conversion specifier, applies a size_t or the corresponding signed integer type to the object pointed to by the argument.

Conversion Specifiers

The following table describes the valid conversion specifiers.

c

Matches a sequence of characters in the input stream of the length specified by the field width, or a single character if no field width is specified.

  • If no l (ell) length modifier is present, the argument is a pointer to an array of char, signed char, or unsigned char large enough to contain the sequence. No null-terminating byte is added.

  • If an l (ell) qualifier is present, the argument is a pointer to an array of wchar_t, which is of sufficient size to contain the sequence. No null-terminating wide character is added.

d

Matches a decimal integer, consisting of an optional sign, followed by one or more decimal digits. If no size modifier is specified, assumes the argument is a pointer to an int.

i

Matches an optional signed integer, whose format is the same as expected for the string argument of the strtol function, with 0 for the base argument. If no size modifier is specified, assumes the argument is a pointer to int.

o

Matches an optional signed octal integer, whose format is the same as expected for the string argument of the strtoul function, with 8 for the base argument. If no size modifier is specified, assumes the argument is a pointer to unsigned.

u

Matches an optional signed decimal integer, whose format is the same as expected for the string argument of the strtoul function, with 10 for the base argument. If no size modifier is specified, assumes the argument is a pointer to unsigned.

x

Matches an optional signed hexadecimal integer, whose format is the same as expected for the string argument of the strtoul function, with 16 for the base argument. If no size modifier is specified, assumes the argument is a pointer to unsigned.

a, e, f, g

Matches a floating-point number, infinity, or NaN, whose format is the same as expected for the string argument of the strtod function. If no size modifier is specified, assumes the argument is a pointer to float.

s

Matches a sequence of non whitespace characters.

If no l (ell) qualifier is present, the argument is a pointer to an array of char, signed char, or unsigned char large enough to contain the sequence and a null-terminating character, which is added by the conversion.

If an l (ell) qualifier is present, the argument is a pointer to an array of wchar_t, which is of sufficient size to contain the sequence and a null-terminating character, which is added by the conversion.

[

Matches a sequence of characters, from a set of expected characters, called the scanset.

  • If no l (ell) length modifier is present, the argument is a pointer to an array of char, signed char, or unsigned char large enough to contain the sequence and a null-terminating character, which is added by the conversion.

  • If an l (ell) qualifier is present, the argument is a pointer to an array of wchar_t, which is of sufficient size to contain the sequence and a null-terminating character, which is added by the conversion.

The scanset includes all the characters from the left square bracket [ up to and including the matching right square bracket ] unless the left square bracket is followed by a circumflex, for example [^. In this case, the scanset contains all the characters that do not appear between the square brackets.

If the conversion specification starts with [ ] or [^], the ] is included in the scanset. For example, to scan for ], the complete conversion specification is %[ ] ].

p

Matches a hexadecimal integer, as described for the x conversion specifier. The converted value is further converted to a value of type void* and then assigned to the object pointed to by the argument.

n

No input data is processed. Instead, the number of characters that have already been read is assigned to the object of type integer that is pointed to by the argument. The %n conversion specifier does not increment the count.

C

Equivalent to the lc conversion specifier.

S

Equivalent to the ls conversion specifier.

%

Matches a single % wide character. The complete conversion specification is %%.

A, E, F, G, X

Equivalent to, respectively, a, e, f, g, and x.

A conversion type specifier other than those listed above causes scanning to terminate and the function to return.

Examples

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

20.1.3 Standard File Open Modes

The fdopen and fopen functions use the following strings to specify the open mode for the file.

String

Description

r

Opens a text file for reading.

w

Creates a text file for writing. If the file exists, fopen truncates the file to zero length.

a

Appends; opens or creates text file for writing at end-of-file.

rb

Opens binary file for reading.

wb

Creates binary file for writing. If the file exists, fopen truncates the file to zero length.

ab

Appends; opens or creates binary file for writing at end-of-file.

r+

Opens file for update (reading and/or writing).

w+

Creates file for update. If the file exists, fopen truncates the file to zero length.

a+

Appends; opens or creates file for update, writing at end-of-file.

r+b

Opens binary file for update (reading and/or writing).

w+b

Creates binary file for update. If the file exists, fopen truncates the file to zero length.

a+b

Appends; opens or creates binary file for update, writing at end-of-file.

rb+

Opens binary file for update (reading and/or writing).

wb+

Creates binary file for update. If the file exists, fopen truncates the file to zero length.

ab+

Appends; opens or creates binary file for update, writing at end-of-file.