C Reference ยท stdio.h
C fprintf() and dprintf() functions
Reference for C fprintf() and POSIX dprintf(): formatted output to any FILE stream or file descriptor, stderr usage, return values and errors.
fprintf() is printf() with a destination. It formats text exactly like printf() and writes it to the FILE * stream you pass as the first argument: a file you opened, stdout, or stderr. In fact printf(...) is defined as fprintf(stdout, ...).
dprintf() is the POSIX version that writes to a raw file descriptor (a small integer such as 1 for standard output) instead of a FILE *. It is not part of ISO C, so on glibc with -std=c23 you must define a feature test macro first.
#include <stdio.h> int fprintf(FILE *restrict stream, const char *restrict format, ...); int dprintf(int fd, const char *restrict format, ...); /* POSIX */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int fprintf(FILE *restrict stream, const char *restrict format, ...); int dprintf(int fd, const char *restrict format, ...);
| Name | Description |
|---|---|
stream |
An open, writable FILE * (stdout, stderr, or the result of fopen). |
fd |
An open file descriptor for writing (POSIX only). |
format, ... |
The same format string and arguments as printf(). |
Returns: the number of characters written, or a negative value on an output or encoding error. For fprintf the stream's error indicator is also set (check it with ferror).
Errors / undefined behavior: a null or closed FILE *, a stream opened read-only, and mismatched format specifiers are undefined behavior or errors. errno is set for most I/O failures.
Usage notes
stderris unbuffered. Error messages appear immediately, even ifstdoutstill has pending text. When both go to the same terminal, callfflush(stdout)first if their relative order matters, as in the first example.- Always check the return value when losing data matters (for example when writing a file): a negative result means the write failed, such as when the disk is full. Checking
fclose()as well catches errors delayed by buffering. - Never pass untrusted text as the format. Use
fprintf(f, "%s", text). dprintfbypassesFILEbuffering. It formats into a temporary buffer and writes directly to the descriptor, so do not mix it carelessly with bufferedFILEwrites on the same descriptor; the order of output can change.dprintfneeds_POSIX_C_SOURCE 200809L(or_GNU_SOURCE) defined before the first include under-std=c23with glibc. It is part of POSIX.1-2008 and not available in strict ISO C environments such as some embedded libraries.- Wide streams.
fwprintffrom<wchar.h>is the wide-character counterpart; a stream cannot be used with both narrow and wide functions. - Formatting into a string is a different function: see
sprintfandsnprintf, andvfprintf, which takes ava_listso you can forward the arguments of your own variadic function. - Text vs. binary.
fprintfwrites text. For raw bytes or structures usefwrite().