Go to the first, previous, next, last section, table of contents.

printf, fprintf, sprintf---format output


#include <stdio.h>

int printf(const char *format [, arg, ...]);
int fprintf(FILE *fd, const char *format [, arg, ...]);
int sprintf(char *str, const char *format [, arg, ...]);

printf accepts a series of arguments, applies to each a format specifier from *format, and writes the formatted data to stdout, terminated with a null character. The behavior of printf is undefined if there are not enough arguments for the format. printf returns when it reaches the end of the format string. If there are more arguments than the format requires, excess arguments are ignored.

fprintf and sprintf are identical to printf, other than the destination of the formatted output: fprintf sends the output to a specified file fd, while sprintf stores the output in the specified char array str. For sprintf, the behavior is also undefined if the output *str overlaps with one of the arguments. format is a pointer to a charater string containing two types of objects: ordinary characters (other than %), which are copied unchanged to the output, and conversion specifications, each of which is introduced by %. (To include % in the output, use %% in the format string.) A conversion specification has the following form:


The fields of the conversion specification have the following meanings:

sprintf returns the number of bytes in the output string, save that the concluding NULL is not counted. printf and fprintf return the number of characters transmitted. If an error occurs, printf and fprintf return EOF. No error returns occur for sprintf.

The ANSI C standard specifies that implementations must support at least formatted output of up to 509 characters.

Supporting OS subroutines required: close, fstat, isatty, lseek, read, sbrk, write.

Go to the first, previous, next, last section, table of contents.