I don't know what your desired scenario would be to use fprintf. In case it is to process errors, the suggested way to print errors in C is by using the perror function.
This function will print your error to stderr coupled with the interpreted errno returned from the previous function. This error is thread-local, i.e., every thread has its private value. For instance, if you try to write to a file inside of a protected directory, like in this function:
int main() {
FILE *file = fopen("/some_write_protected_dir/file.txt", "w");
if (file == NULL) {
perror("Error creating file");
} else {
fclose(file);
}
return 0;
}
It will print Error creating file: Permission denied. Notice that it added the interpreted text for errno 13. Also, make sure not to have any call to other syscalls as the errno value will be overwritten.
The fprintf(stderr, ...) function on the other hand can be used to print your custom error messages.
fprintf.$ ./a.out 1>&2