PRINTF(3S) UNIX Programmer's Manual PRINTF(3S) NAME printf, fprintf, sprintf - output formatters SYNOPSIS #include int printf (format [ , arg ] ... ) char *format; int fprintf (stream, format [ , arg ] ... ) FILE *stream; char *format; int sprintf (s, format [ , arg ] ... ) char *s, *format; HP-UX COMPATIBILITY Level: HP-UX/RUN ONLY Origin: System V DESCRIPTION _P_r_i_n_t_f places output on the standard output stream _s_t_d_o_u_t. _F_p_r_i_n_t_f places output on the named output _s_t_r_e_a_m. _S_p_r_i_n_t_f places "output", followed by the null character (\0) in con- secutive bytes starting at *_s; it is the user's responsibil- ity to ensure that enough storage is available. Each func- tion returns the number of characters transmitted (not including the \0 in the case of _s_p_r_i_n_t_f), or a negative value if an output error was encountered. Each of these functions converts, formats, and prints its _a_r_gs under control of the _f_o_r_m_a_t. The _f_o_r_m_a_t is a character string that contains two types of objects: plain characters, which are simply copied to the output stream, and conversion specifications, each of which results in fetching of zero or more _a_r_gs. The results are undefined if there are insuffi- cient _a_r_gs for the format. If the format is exhausted while _a_r_gs remain, the excess _a_r_gs are simply ignored. Each conversion specification is introduced by the character %. After the %, the following appear in sequence: Zero or more _f_l_a_g_s, which modify the meaning of the conversion specification. An optional decimal digit string specifying a minimum _f_i_e_l_d _w_i_d_t_h. If the converted value has fewer charac- ters than the field width, it will be padded on the left (or right, if the left-adjustment flag (see below) has been given) to the field width; A _p_r_e_c_i_s_i_o_n that gives the minimum number of digits to appear for the d, o, u, x, or X conversions, the number of digits to appear after the decimal point for the e and f conversions, the maximum number of significant digits for the g conversion, or the maximum number of characters to be printed from a string in s conversion. The precision takes the form of a period (.) followed by a decimal digit string: a null digit string is treated as zero. An optional l specifying that a following d, o, u, x, or X conversion character applies to a long integer _a_r_g, or an optional h specifying that a following d, o, u, x, or X conversion character applies to a short integer _a_r_g. A character that indicates the type of conversion to be applied. A field width or precision may be indicated by an asterisk (*) instead of a digit string. In this case, an integer _a_r_g supplies the field width or precision. The _a_r_g that is actually converted is not fetched until the conversion letter is seen, so the _a_r_gs specifying field width or preci- sion must appear _b_e_f_o_r_e the _a_r_g (if any) to be converted. The flag characters and their meanings are: - The result of the conversion will be left- justified within the field. + The result of a signed conversion will always begin with a sign (+ or -). blank If the first character of a signed conversion is not a sign, a blank will be prepended to the result. This implies that if the blank and + flags both appear, the blank flag will be ignored. # This flag specifies that the value is to be con- verted to an "alternate form." For c, d, s, and u conversions, the flag has no effect. For o conversion, it increases the precision to force the first digit of the result to be a zero. For x (X) conversion, a non-zero result will have 0x (0X) prepended to it. For e, E, f, g, and G conversions, the result will always contain a decimal point, even if no digits follow the point (normally, a decimal point appears in the result of these conversions only if a digit follows it). For g and G conversions, trailing zeroes will _n_o_t be removed from the result (which they normally are). The conversion characters and their meanings are: d,o,u,x,X The integer _a_r_g is converted to signed decimal, unsigned octal, decimal, or hexadecimal notation (x and X), respectively; the letters abcdef are used for x conversion and the letters ABCDEF for X conversion. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeroes. The default precision is 1. The result of converting a zero value with a precision of zero is a null string (unless the conversion is o, x, or X _a_n_d the # flag is present). f The float or double _a_r_g is converted to decimal notation in the style "[-]ddd.ddd", where the number of digits after the decimal point is equal to the precision specification. If the precision is missing, 6 digits are output; if the precision is explicitly 0, no decimal point appears. e,E The float or double _a_r_g is converted in the style "[-]d.ddde+_ddd", where there is one digit before the decimal point and the number of digits after it is equal to the precision; when the precision is missing, 6 digits are produced; if the preci- sion is zero, no decimal point appears. The E format code will produce a number with E instead of e introducing the exponent. The exponent always contains exactly three digits. g,G The float or double _a_r_g is printed in style f or e (or in style E in the case of a G format code), with the precision specifying the number of signi- ficant digits. The style used depends on the value converted: style e will be used only if the exponent resulting from the conversion is less than -4 or greater than the precision. Trailing zeroes are removed from the result; a decimal point appears only if it is followed by a digit. c The character _a_r_g is printed. s The _a_r_g is taken to be a string (character pointer) and characters from the string are printed until a null character (\0) is encountered or the number of characters indicated by the pre- cision specification is reached. If the precision is missing, it is taken to be infinite, so all characters up to the first null character are printed. % Print a %; no argument is converted. In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is simply expanded to contain the conversion result. Characters generated by _p_r_i_n_t_f and _f_p_r_i_n_t_f are printed as if _p_u_t_c_h_a_r had been called (see _p_u_t_c(3S)). EXAMPLES To print a date and time in the form "Sunday, July 3, 10:02", where _w_e_e_k_d_a_y and _m_o_n_t_h are pointers to null- terminated strings: printf("%s, %s %d, %.2d:%.2d", weekday, month, day, hour, min); To print _p_i to 5 decimal places: printf("pi = %.5f", 4*atan(1.0)); SEE ALSO ecvt(3C), putc(3S), scanf(3S), stdio(3S).