I know that both of the functions can be used to output to the console.
I read that question, but no one didn't tell which is prefered to be used when outputing to the console.
So which function is better, are there any major differences?
Summary – printf vs fprintf The difference between printf and fprintf is that printf is used to print a formatted string to a standard output which is most of the time a computer screen and fprintf is used to print a formatted string to a specific file. printf and fprintf can be used according to the task.
printf function is used to print character stream of data on stdout console. fprintf: fprintf is used to print the string content in file but not on stdout console.
fprintf writes formatted text to the output stream you specify. printf is equivalent to writing fprintf(stdout, ...) and writes formatted text to wherever the standard output stream is currently pointing. sprintf writes formatted text to an array of char , as opposed to a stream.
fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...);
To quote the standard (7.21.6.3 in n1570):
The
printf
function is equivalent tofprintf
with the argumentstdout
interposed before the arguments toprintf
.
So printf
is more convenient when printing to the console, otherwise, there's no difference. But fprintf
is a bit easier to modify if you want to change the output target.
Each process has an input stream, named stdin
and two output streams, stdout
and stderr
. These output streams are both connected to your terminal so the following commands will all print the line "hello" to your terminal:
printf("hello\n");
fprintf(stdout, "hello\n");
fprintf(stderr, "hello\n");
The first two are exactly the same, the first just being shorter and more convenient. The first is most commonly used.
The third is different in that the content sent to stderr
is logically separate from that sent to stdout
. It is usually used for error messages that you want the user to see. The library function perror
prints its error messages to stderr
.
The significance of the stderr
stream being logically separate is that its content can be separated from stdout
. For example, say we use the command ls -l
to list a file.
$ touch myfile
$ ls -l myfile
-rw-r--r-- 1 wrm staff 0 6 Nov 20:44 myfile
Now if we redirect the output of ls
to another file, we see the following:
$ ls -l myfile > otherfile
$
There is no output printed because the >
redirected the stdout
stream of the ls
process to otherfile
. You can see the output it redirected by looking at otherfile
:
$ cat otherfile
-rw-r--r-- 1 wrm staff 0 6 Nov 20:44 myfile
$
But the >
did not redirect the stderr
stream. You can test that by removing myfile
and re-running the redirected ls -l
command:
$ rm myfile
$ ls -l myfile > otherfile
ls: myfile: No such file or directory
$
So here you can see that although stdout
was redirected to otherfile
, stderr
was not redirected and so its content appeared on the terminal. Also note that otherfile
is now empty because the ls
command did not find myfile
and so there was nothing to send to stdout
.
It is also possible to redirect stderr
, but it depends upon your shell (the program that controls your terminal) how that is done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With