HP-UX Reference (11i v1 05/09) - 3 Library Functions N-Z (vol 7)

p
printf(3S) printf(3S)
EXTERNAL INFLUENCES
Locale
The LC_CTYPE category affects the following features:
Plain characters within format strings are interpreted as single byte and/or multibyte characters.
Field width is given in terms of bytes. As characters are placed on the output stream, they are
interpreted as single byte or multibyte characters and the field width is decremented by the length
of the character.
Precision is given in terms of bytes. As characters are placed on the output stream, they are inter-
preted as single byte or multibyte characters and the precision is decremented by the length of the
character.
The return value is given in terms of bytes. As characters are placed on the output stream, they
are interpreted as single byte or multibyte characters and the byte count that makes up the return
value is incremented by the length of the character.
The LC_NUMERIC category determines the radix character used to print floating-point numbers, and the
thousands’ grouping characters if the grouping flag
is on.
International Code Set Support
Single byte character code sets are supported. Multibyte character code sets are also supported as
described in the
LC_CTYPE category above.
RETURN VALUE
Each function returns the number of bytes transmitted (excluding the \0 in the case of sprintf()
or a
negative value if an output error was encountered.
ERRORS
printf(), and fprintf() fail if either the stream is unbuffered or stream’s buffer needed to be flushed
causing an underlying
write() call to be invoked (see write(2)), and:
[EAGAIN] The O_NONBLOCK flag is set for the file descriptor underlying stream and the process
would be delayed in the write operation.
[EBADF] The file descriptor underlying stream is not a valid file descriptor open for writing.
[EFBIG] An attempt was made to write to a file that exceeds the processs file size limit or the
maximum file size (see ulimit(2)).
[EINTR] A signal was caught during the
write() system call.
[EIO] The process is in a background process group and is attempting to write to its control-
ling terminal, TOSTOP is set, the process is neither ignoring nor blocking the
SIGTTOU signal, and the process group of the process is orphaned.
[ENOSPC] There was no free space remaining on the device containing the file.
[EPIPE] An attempt is made to write to a pipe or FIFO that is not open for reading by any pro-
cess. A SIGPIPE signal is also sent to the process.
[EILSEQ] The data obtained from the input stream does not form a valid wide character.
[ENOMEM] Insufcient storage space is available.
Additional errno values can be set by the underlying write() function (see write(2)).
EXAMPLES
To print a date and time in the form "Sunday, July 3, 10:02", where weekday and month are pointers to
null-terminated strings:
printf("%s, %s %d, %d:%.2d", weekday, month, day, hour, min);
To print π to 5 decimal places:
printf("pi = %.5f", 4 * atan(1.0));
To create a language independent date-and-time printing routine write:
printf(format,weekday,month,day,hour,min,2,2);
HP-UX 11i Version 1: September 2005 4 Hewlett-Packard Company Section 3665