HP-UX Reference (11i v3 07/02) - 3 Library Functions N-Z (vol 7)

p
printf(3S) printf(3S)
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 null character in the case of
sprintf()) or a negative value if an output error was encountered.
By default, snprintf() returns a negative value if maxsize is smaller than the number of characters for-
matted. In the UNIX 2003 standards environment (see standards(5)) it returns the the number of bytes
that would have been written to buffer s, excluding the terminating null byte, if maxsize had been
sufficiently large.
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 max-
imum 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 controlling
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 process.
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] Insufficient 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 Pi 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);
For American usage, format would point to the string:
"%1$s, %2$s %3$d, %4$*6$.*7$d:%5$*6$.*7$d"
and result in the output:
"Sunday, July 3, 10:02"
For German usage, the string:
"%1$s, %3$s %2$d, %4$*6$.*7$d:%5$*6$.*7$d"
results in the output:
HP-UX 11i Version 3: February 2007 5 Hewlett-Packard Company 137