The PyMiers

Sunday 5 June 2011

Format I/O of ...printf function

The ...printf functions
The printf and fprintf function write a variable number of data items to an input stream, using a format string to control the appearance of the output. The propertypes for both function end with the … symbol (an ellipsis), which indicates a variable number of additional arguments. Both functions return a number of characters written; a negative return value indicates that an error occurred.
The only different between printf and fprintf function is that printf always writes to stdout (the standard output stream), whereas fprintf wirtes to the stream indicated by its the first argument:
printf(“abc”); // writes to stdout
fprintf(fp,”def”); // writes to fp

A call of printf is equivalent to a call of fprintf with stdout as the first argument.
Don't think of fprintf as merely a function that writes data to disk files, though. Like many function int , fprintf works fine with any output stream. In fact, one of the most common use of fprintf – writing error messages to stderr (the standard error stream), has nothing to do with disk files. Here's what such a call might look like:
Writing the message to stderr gaurantees that is will appear on the screen even if the user redirects stdout.
There are two other functions in that can write formated output to a stream. These functions, named vfprintf and vprintf, are fairly obscure. Both rely on the va_list type, which is declared in .
...printf Conversion Specifications
Both printf and fprintf require a format string containing ordinary characters and/or conversion specifications. Ordinary characters are printed as is; conversion specifications describe how the remaining arguments are to be converted to character form for display.
A ...printf conversion specification consists of the % character, followed by as many as five distinct items:

Here's a detailed description of these items, which must appear in the order shown:
Flags (optional; more than one permitted). The – flag causes left justification within a field; the other flags affect the way number are displayed. Following table gives a complete list of flags:
Left-justify within field. (The default is right justification
Numbers produced by signed conversions always begin with + or - . (Normally, only negative numbers are preceded by a sign.)
Nonnegative numbers produced by signed conversions are preceded by a space.
(The + flag override the space flag.)
Octal numbers begin with 0, nonzero numbers hexadecimal begin with 0x or 0X. Floating-point numbers always have a decimal point. Trailing zeros aren't removed from numbers printed with the g or G conversions.
Numbers are padded with leading zeros up to the field width. The 0 flag is ignored if the conversion is d, i, o, u, x or X and a precision is specified. (The – flag overrides the 0 flag.)

Minimum field width (optional): An item that's too small to occupy this number of characters will be padded. (By default, space are padded to the left of the item, thus right-justifying is within the field.) An item that's too large for interger or the characters *. If * is present, the field width is obtained from the next argument. If this argument is negative, it's treated as a positive number preceded by a – flag.
Precision (optional): The meaning of the precision depends on the conversion:
d, i, o, u, x, X: minimum number of digits (leading zeros are added if the number has fewer digits)
A, a, e, E, f, F: number of digits after the decimal point
G, g: number of significant digits
s: maximum number of bytes
The precision is a period followed by an interger of the characters *. If * is present, the precision is obtained from the next argument. (If this argument is negative, the effect is the same as not specifying a precision.) If only the period is presented, the precision is zero.
Lenght modifier (optional): The presence of a length modifier indicates that the item to be displayed has a type that's longer or shorter than is normal for a particular conversion specification. (For example, %d normally refers to an int value; %hd is used to display a short int and %ld is used to display a long int.) Following table lists each length modifier, the conversion specifiers with which may used, and the type indicated by the combination of the two. (Any combination of length modifier and conversion specifier not show in the table causes undefined behavior.)
Conversion Specifications
d, i, o, u, x, X
Signed char, unsigned char

Signed char *
d, i, o, u, x, X
Short int, unsigned short int

Short int *
d, i, o, u, x, X
Long int, unsigned long int

Long int *


wchar_t *

A, a, e, E, f, F, g, G
No effect
d, i, o, u, x, X
Long long int, unsigned long long int

Long long int *
d, i, o, u, x, X
intmax_t, uintmax_t

intmax_t *
d, i, o, u, x, X

size_t *
d, i, o, u, x, X

ptrdiff_t *
A, a, e, E, f, F, g, G
Long double
(*) C99 only
Conversion specifier: The conversion specifier must be one of the characters listed in following table:
d, i
Convert an int value to decimal form.
o, u, x, X
Convert an unsigned int value to base 8 (o), base 10(u), or base 16 (x or X). x displays the hexadecimal digits a-f in lower case; X displays them in upper case.
F(*), f
Convert a duoble value to decimal form, putting the decimal point in the correct position. If no pricision is specified, displays the six digits after the dicimal point.
E, e
Convert a duouble value to scientific notation. If no precision is specified, displays six digits after the decimal point. If e is chosen, the exponent is preceded by the letter e; if E is chosen, the exponent is preceded by E.
G, g
g convet a double value to either f form or e form. e form is selected if the number's exponent is less than 4 or greater than or equal to the precision. Trailing zeros are not displayed (unless # flag is used); a decimal point appears only when followed by digit. G choose between F and E forms
A(*), a(*)
Converts a double value to hexadecimal scientific notation using the form [-]0xh.hhhh, where [-] is an optional minus sign, the h's represent hex digits, +- is either a plus or minus sign, and d is the exponent, d is a decimal number that represents a power of 2. If no precision is specified, enough digits are display after the decimal point to present the exact value of the number (if posible). a displays the hex digits a-f in lower case; A displays them in upper case. The choice of a or A also affects the case of the letters x and p.
Displays an int value as an unsigned char
Writes the characters pointed to by the argument. Stops writing when the number of bytes specified by the precision (if present) is reached or the null character is encountered.
Convert a void * value to printabe form.
The corresponding argument must point to an object of type int. Stores in this object the number of characters written so far by this call of ...printf; produces no output.
Writes the character %.
(*) C99 only


  1. :)) post lên đây mất ảnh và mất bảng =))

  2. đề nghị sử dụng tính năng readmore khi post và chọn tag cho bài viết để nâng cao tính chuyên nghiệp!