|||
This part is from the book The C Programming Language. These are popular functions in stdio.h
1. Formatted ouput
int fprintf(FILE *stream, const char *format, ...)
fprintf converts and writes outputs to stream under the control of format. The return value is the number of characters written, or negative if an error occurred.
The format string contains two type of objects: odrdinary characters, which are copied to the output stream, and convension specificatons. each of which causes conversion and printing of the next successive argument of printf. Each conversion specification begins with the character % and ends with a conversion character. Between the % and the conversion character there may be, inorder:
a) flags:
-, which spcifies left adjustment of converted argument in its field.
+, which specfies that the number will always be printed with a sign.
space, if the first character is not a sign, a space will be prefixed.
0, for numeric conversions, specifies padding to the field width with leading zeros.
#, which specifies an alternate output form. For o, the first digit will become zero. For x or X, 0x or 0X will be prefixed to a non-zero result. For e, E, f, g and G, the output will always have a decimal point; for gand G, trailling zeros will not be removed.
b) A number specifying a minimum field width. The converted argument will be printed in a field at least this wide, and wider if necessary. If the converted argument has fewer characters than the field width it will be padded on the left (or right, if left adjustment has been requested) to make up the field width. The padding character is normally space, but is 0 if the zero padding flag is present.
c) A period, which separates the field width from the precision.
d) A number, the precision, that specifies the maximum number of characters to be printed from a string, or number of digits to be printed after the decimal point for f, e or E conversions, or the number of significant digits for g or G conversion, or the number of digits to be printed for an integer (leading 0s will be added to make up the necessary width).
e) A length modifier h, l or L. "h" indicates that the corresponding argument is to be printed as a short or unsigned short; "l" indicates that the argument is a long or unsigned long, "L" indicates that the argument is a long double.
Width or precision or both may be specified as *, in which case the value is computed by converting the next argument(s), which must be int.
int printf(const char *format, ...)
printf(...) is equivalent to fprintf(stdout, ...).
int sprintf(char *s, const char *format, ...)
sprintf(...) is the same as printf except that the output is written into the string s, testrminated with '