Write formatted output to stdout
#include <stdio.h> int printf( const char * format, ... );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The printf() function writes output to the stdout stream, under control of the argument format.
setlocale( LC_CTYPE, "C-TRADITIONAL" );
before calling printf(), the locale switches multibyte processing from UTF-8 to 1-to-1, and printf() safely transfers the misformed multibyte characters.
Format Arguments
If there are leftover arguments after processing format, they're ignored.
The printf() family of functions allows for language-dependent radix characters. The default character is ".", but is controlled by LC_NUMERIC and setlocale().
Format control string
The format control string consists of:
The valid format control flags are:
If you don't specify a field width, or if the given value 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 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 * (or 0*), 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 you give a precision specifier of *, but there's no precision value in the argument list, a precision of 0 is used.
The precision value affects the following conversions:
A type length specifier affects the conversion as follows:
Note that, although the argument may have been promoted to an int as part of the function call, the value is converted to the smaller type before it's formatted.
The valid conversion type specifiers are:
The a conversion uses the letters abcdef and produces x and p; the A conversion ABCDEF, X and P. The exponent always has one digit, even if it's 0, and no more digits than necessary. The values for infinity or NaN are converted in the style of an f or F.
An l ("el") qualifier causes a wint_t argument to be converted as if by an ls conversion into a wchar_t, the first element being the wint_t and the second being a null wide character.
If the argument is nonzero, the digit before the decimal-point character is nonzero. The precision is used as the number of digits following the decimal-point character. If you don't specify the precision, a default precision of six is used. If the precision is 0, the decimal-point character is suppressed. The value is rounded to the appropriate number of digits.
The exponent sign and the exponent (that indicates the power of ten by which the decimal fraction is multiplied) are always produced. The exponent is at least two digits long and has only as many additional digits as necessary to represent it. If the value is zero, the exponent is zero.
For E conversions, the exponent begins with the character E, rather than e.
The arguments infinity or NaN are converted in the style of the f or F conversion specifiers.
The precision is used as the number of digits following the decimal-point character. If you don't specify the precision, a default precision of six is used. If the precision is 0, the decimal-point character is suppressed; otherwise, at least one digit is produced before the decimal-point character. The value is rounded to the appropriate number of digits.
An argument of type double that represents infinity or NaN is converted to [-]inf or [-]nan. The F specifier produces [-]INF or [-]NAN.
Arguments representing infinity or NaN are converted in the style of the f or F conversion specifiers.
If you use an l ("el") qualifier, the argument is interpreted as a pointer to a wchar_t array, and each wide character, including the terminating NUL, is converted as if by a call to wcrtomb(). The terminating NUL is written only if you don't specify the precision, or if you specify the precision and the length of the character sequence is less than the precision.
Hexadecimal notation uses the digits 0 through 9 and the characters a through f or A through F for x or X conversions, respectively, as the hexadecimal digits. Subject to the alternate-form control flag, 0x or 0X is prepended to the output.
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 will occur.
If the value corresponding to a floating-point specifier is infinity, or not a number (NAN), then the output will be inf or -inf for infinity, and nan or -nan for NANs.
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 number of characters written, excluding the terminating NULL, or a negative number if an error occurred (errno is set).
#include <stdio.h> #include <stdlib.h> int main( void ) { char *weekday, *month; weekday = "Saturday"; month = "April"; printf( "%s, %s %d, %d\n", weekday, month, 10, 1999 ); printf( "f1 = %8.4f f2 = %10.2E x = %#08x i = %d\n", 23.45, 3141.5926, 0x1db, -1 ); return EXIT_SUCCESS; }
produces the output:
Saturday, April 10, 1999 f1 = 23.4500 f2 = 3.14E+003 x = 0x0001db i = -1
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |