Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdio.h>

int fprintf(FILE *restrict stream, const char *restrict format, ...);
int dprintf(int fd, const char *restrict format, ...);   /* POSIX */

Try it

C Writing to stdout, stderr and a file

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.

C dprintf with a file descriptor

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

C
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

  • stderr is unbuffered. Error messages appear immediately, even if stdout still has pending text. When both go to the same terminal, call fflush(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).
  • dprintf bypasses FILE buffering. It formats into a temporary buffer and writes directly to the descriptor, so do not mix it carelessly with buffered FILE writes on the same descriptor; the order of output can change.
  • dprintf needs _POSIX_C_SOURCE 200809L (or _GNU_SOURCE) defined before the first include under -std=c23 with glibc. It is part of POSIX.1-2008 and not available in strict ISO C environments such as some embedded libraries.
  • Wide streams. fwprintf from <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 sprintf and snprintf, and vfprintf, which takes a va_list so you can forward the arguments of your own variadic function.
  • Text vs. binary. fprintf writes text. For raw bytes or structures use fwrite().