Which is preferable - printf or fprintf

16,227

Solution 1

To quote the standard (7.21.6.3 in n1570):

The printf function is equivalent to fprintf with the argument stdout interposed before the arguments to printf.

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.

Solution 2

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.

Solution 3

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.

Share:
16,227
Lior
Author by

Lior

SOreadytohelp

Updated on July 11, 2022

Comments

  • Lior
    Lior almost 2 years

    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?

  • Tal Aloni
    Tal Aloni almost 7 years
    unfortunately, this is not always the case, the ARM v7 LEDE toolchain will circumvent printf, while fprintf(stdout, ...) will work properly.
  • Daniel Fischer
    Daniel Fischer almost 7 years
    That means it's not a conforming implementation, doesn't it?