printf
Writes formatted output to a specified file designated by stdout
#include <stdio.h>
int printf (
const char *format,
... );
The printf function returns the number of characters written, or it returns a negative value if an output error occurred. If an error occurs, errno is set.
The printf function writes output to the file designated by stdout under control of the argument format.
The format control string consists of ordinary characters, which are written exactly as they occur in the format string, and of conversion specifiers, which cause argument values to be written as they are encountered during the processing of the format string. An ordinary character in the format string is any character, other than a percent (%) character, that is not part of a conversion specifier. A conversion specifier is a sequence of characters in the format string. The conversion specifier begins with a % and is followed, in sequence, by the following:
h l L N F
c d e E f F g G i n o p s u x X
The valid format control flags are:
If no field width is specified, or if the value that is given is less than the number of characters in the converted value (subject to any precision value), a field of sufficient width to contain the converted value is used. If the converted value has fewer characters than are specified by the field width, the value is padded on the left (or right, subject to the left-justification flag) with spaces or zero characters (0). If the field width begins with a zero, the value is padded with zeros; otherwise, the value is padded with spaces. If the field width is *, a value of type int from the argument list is used (before a precision argument or a conversion argument) as the minimum field width. A negative field width value is interpreted as a left- justification flag, followed by a positive field width.
As with the field width specifier, a precision specifier of * causes a value of type int from the argument list to be used as the precision specifier. If no precision value is given, a precision of 0 is used. The precision value affects the following conversions:
A type length specifier affects the conversion as follows:
The valid conversion type specifiers are:
Any other conversion type specifier character, including another percent ( %) character, is written to the output stream with no special interpretation.
The arguments must correspond with the conversion type specifiers, left to right in the string; otherwise, indeterminate results occur.
For example, a specifier of the form %8.*f defines a field to be at least 8 characters wide and gets the next argument for the precision to be used in the conversion.
The output from
printf ("f1 = %8.4f f2 = %10.2E x = %#08x i = %d", 23.45, 3141.5926, 0x1db, -1 );
would be
f1 = 23.4500 f2 = 3.14E+003 x = 0x0001db i = -1
You can also use strings similar to the following:
printf ("Test: %3$s %2$d %1$s", string, 10, string);
#include <stdio.h> main () { char *weekday, *month; int day, year; weekday = "Saturday"; month = "April"; day = 18; year = 1991; printf ("%s, %s %d, %d\n", weekday, month, day, year); }
produces the following:
Saturday, April 18, 1991