Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
93
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Part IV • Documenting the Differences

wctomb()

Header:

stdlib.h

Syntax:

int wctomb(char * szDestination, const wchar_t

 

WideChar);

Description:

Converts a single wide character in the buffer pointed to by

 

pWideChars to a multibyte character.

Parameters:

szDestination—Pointer to a buffer to receive the multibyte

 

characters.

 

WideChar—Wide character to be converted.

Returns:

Number of bytes resulting from the conversion.

Example:

wctomb(szBuffer, szWideChar[0]);

Note:

See wcstombs().

printf() Format Codes

The printf() family of functions—printf(), fprintf(), sprintf(), vprintf(),

vfprintf(), and vsprintf()— uses a format string to describe the format of the output. This format string enables the programmer to specify what is output.

The format string specifies the variables and how they are output. Because these functions use a variable number of arguments, the function doesn’t know what variables have been passed except to look at the format string. Making an error in one variable’s type often causes problems with the variables that follow.

How is a variable formatted? The first character in a format specifier is a percent sign, %. This format specifier has the following fields:

%[flags][size][.precision][prefix]type

The flags field is optional. Four values are allowed in this field, as shown in Table 14.8.

606

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

Table 14.8. Flags used with printf() family of functions.

Flag character

Description

‘-’

‘+’

Left justify the output field within the width defined.

Signed, positive values are always prefixed with a plus sign. Negative values are prefixed with a minus sign.

‘ ‘

Signed, positive values are always prefixed with a blank.

 

Negative values are prefixed with a negative sign.

‘#’

Alternate conversion adds a leading zero for octal values,

 

a ‘0x’ or ‘0X’ for hexadecimal values, forces a floating

 

point number to always have a decimal point, and

 

removes floating point trailing zeros.

 

 

The following section describes each type of printf() field, and the meanings for size, .precision, and prefix fields. Because all these fields depend on the type field, the table is organized with subheads for each type.

c

The c type tells printf() to output a single character. This field is affected by the following:

flags

Only the - (left justify) flag is used. If the width field specifies a width greater than one, then the character can be either right justified (default) or left justified (using the left justify flag).

width

Specifies the width of the output.

607

Part IV • Documenting the Differences

.precision

Ignored if present.

prefix

Ignored if present.

d and i

The d and i type tells printf() to output a signed decimal integer. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type.

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored, and the formatted output’s actual width is used.

.precision

The precision specifies the minimum number of digits to appear. This causes output that has fewer than the number of digits of width to be padded on the left with zeros.

prefix

The prefix allows specification of a short or long integer. Use h to specify a short (16-bit) integer, and l to specify a long (32-bit) integer. The default is to the default size for an integer for the system.

e and E

The e and E types tell printf() to output a floating point number, using an exponential format. This output takes the form of [-]d.ddde[+|-]ddd. If the E type

608

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

is specified, then the form taken is [-]d.dddE[+|-]ddd with an uppercase E used to indicate the exponent.

This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type.

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored and the formatted output’s actual width is used.

.precision

Specifies the number of digits that follow the decimal point in the mantissa.

prefix

Two prefixes are recognized. The l prefix specifies the value is a double. The L prefix specifies the value is a long double. When a float is passed as a parameter, it is always passed as a double (unless it is cast as a float, which is not recommended).

f

The f type tells printf() to output a floating point number. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type.

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored and the formatted output’s actual width is used.

609

Part IV • Documenting the Differences

.precision

Specifies the number of digits that follow the decimal point.

prefix

Two prefixes are recognized. The l prefix specifies the value is a double. The L prefix specifies the value is a long double. When a float is passed as a parameter, it is always passed as a double (unless it is cast as a float, which is not recommended).

g and G

The g and G types tell printf() to output using either the f, e, or E types, depending on the value of the argument. The e type is used if the exponent for the conversion would be less than -4 or greater than the precision. Trailing zeros are removed, and the decimal point appears only if there is a decimal part of the number. See the f, e, or E types for more information.

n

The n type tells printf() to save the current number of characters written so far to the variable pointed to by the argument. No modifiers are allowed with the n type.

o

The o type tells printf() to output a decimal number in octal (base 8) format. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type.

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored, and the formatted output’s actual width is used.

610

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

.precision

The precision specifies the minimum number of digits to appear. This causes output that has fewer than the number of digits of width to be padded on the left with zeros.

prefix

The prefix allows specification of a short or long integer. Use h to specify a short (16bit) integer and l to specify a long (32-bit) integer. The default is to the default size for an integer for the system.

p and P

The p and P types tell printf() to output a pointer. The pointer is printed in hexadecimal notation, in a format that may be machine dependent. The case of the type is used to specify the case of the hexadecimal digits. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type.

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored, and the formatted output’s actual width is used.

.precision

The precision specifies the minimum number of digits to appear. This causes output that has fewer than the number of digits of width to be padded on the left with zeros.

prefix

With compilers that have segmented memory (and memory models) the prefix allows specification of near or far pointers. Use F to specify a far (or long) pointer and N to specify a near (or short) pointer. The default is to the default pointer type for the program.

611

Part IV • Documenting the Differences

s

The s type tells printf() to output a string. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type except for the + (for plus sign) and the ‘ ‘ (a blank, again for signs).

width

The width specifier defines the minimum width. If the string is longer than the width and no .precision value is specified, then the output expands to the size of the string.

.precision

Specifies the maximum number of characters to be output. If the string being printed is larger than the width specification, then it will be truncated to .precision size.

prefix

With compilers that have segmented memory (and memory models) the prefix allows specification of a near or far pointer. Use F to specify a far (or long) pointer and N to specify a near (or short) pointer. The default is to the default pointer type for the program.

u

The u type tells printf() to output an unsigned decimal integer. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type, except there can never be a minus sign.

612

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored and the formatted output’s actual width is used.

.precision

The precision specifies the minimum number of digits to appear. This causes output that has fewer than the number of digits of width to be padded on the left with zeros.

prefix

The prefix allows specification of a short or long integer. Use h to specify a short (16bit) integer and l to specify a long (32-bit) integer. The default is to the default size for an integer for the system.

x and X

The x and X types tell printf() to output an unsigned, hexadecimal integer. The case of the hexadecimal digits matches the case of the type field. This field is affected by the following:

flags

All flags as shown in Table 14.8 affect a field of this type.

width

Specifies the minimum width. If the formatted result is less than the width, then it is padded with blanks. If the formatted result is greater than the width, then the width is ignored and the formatted output’s actual width is used.

.precision

The precision specifies the minimum number of digits to appear. This causes output that has fewer than the number of digits of width to be padded on the left with zeros.

613

Part IV • Documenting the Differences

prefix

The prefix allows specification of a short or long integer. Use h to specify a short (16bit) integer and l to specify a long (32-bit) integer. The default is to the default size for an integer for the system.

scanf() format codes

The scanf() family of functions—scanf(), fscanf(), and sscanf()—uses a format string to describe the format of the input. This format string allows the programmer to specify what is read. All arguments passed to these functions that are to receive values are passed as addresses. Failure to provide addresses causes the program to fail.

The format string specifies what the variables will be when they are filled in, their types, and the format of the input data. Because these functions use a variable number of arguments, the function doesn’t know what variables have been passed except to look at the format string.

How is a variable formatted? The first character in a format specifier is a percent sign, %. This format specifier has the following fields:

%[*][width][typelength]type

The * specifies that the next field of type is to be skipped. The specification of type is important to ensure the field is correctly skipped.

The optional specification width specifies the maximum width that is scanned for this field. The field may well be shorter than width, depending on the type specification.

The typelength specifier provides information about the size of the object that receives the input. Using an ‘h’ character specifies a short (16-bit) object while the ‘l’ character specifies a long (32-bit) object. When used with floating point arguments, the ‘l’ character specifies a long double object (its size being implementation dependent).

614

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

c

The c type tells scanf() to input one (or more) characters, which may include whitespace characters (the s type does not include whitespace characters). This field is affected by the following:

width

Specifies the width of the input field. If not specified, then the width is assumed to be one character.

typelength

None allowed.

d

The d type tells scanf() to input a signed decimal integer. This field is affected by the following:

width

Specifies the maximum width.

typelength

The typelength specifier provides information about the size of the object that receives the input. Using an ‘h’ character specifies a short (16-bit) object while the ‘l’ character specifies a long (32 bit) object.

o

The o type tells scanf() to input a decimal number in octal (base 8) format. This field is affected by the following:

615