"fprintf"-like function for file descriptors (i.e. int fd instead of FILE* fp)

You could look into dprintf (GNU extensions, not in C or POSIX) :

The functions dprintf() and vdprintf() (as found in the glibc2 library) are exact analogues of fprintf() and vfprintf(), except that they output to a file descriptor fd instead of to a given stream.

EDIT As pointed by several of you in the comments, POSIX 2008 standardized these functions.


There is no C or POSIX (edit: prior to 2008) standard function to do printf on a file descriptor, but you can “open” a file descriptor as a FILE * with the POSIX-standard fdopen(int desc, const char *mode). I'm not sure how well supported flipping back to using the descriptor directly is, but I'm guessing it might work if you flush the buffer first…

Of course you could just implement your own using something like vsprintf, but obviously you then have to take care of the buffering.