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?
-
It is up to you. You are the programmer. You set the preference. – SwiftMango Nov 07 '12 at 01:24
3 Answers
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.

- 181,706
- 17
- 308
- 431
-
1unfortunately, this is not always the case, the ARM v7 LEDE toolchain will circumvent printf, while fprintf(stdout, ...) will work properly. – Tal Aloni Aug 23 '17 at 20:43
-
4
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.

- 3,554
- 2
- 23
- 24
If you need to print to a particular output stream, use fprintf.
If you need to show an error message, use fprintf w/ stderr
If you're developing a command-line executable and just want to display something to the user, use printf.

- 17,560
- 3
- 48
- 51