Which is preferable - printf or fprintf

I know that both functions can be used to output to the console.
I read this question , but no one said what is preferred to use when outputting to the console. So, which function is better, are there any significant differences?

+4
source share
3 answers

To quote the standard (7.21.6.3 in n1570):

The printf function is equivalent to fprintf if the stdout argument is inserted before the printf arguments.

So printf more convenient when printing to the console, otherwise there is no difference. But fprintf little easier to change if you want to change the output target.

+9
source

Each process has an input stream named stdin and two output streams, stdout and stderr . These output streams connect to your terminal, so the following commands will print the string "hello" to your terminal:

 printf("hello\n"); fprintf(stdout, "hello\n"); fprintf(stderr, "hello\n"); 

The first two are exactly the same, the first is simply 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 the content sent to stdout . It is usually used for error messages that you want to see. The perror library perror displays its error messages on stderr .

The meaning of the logically shared stderr stream is that its contents can be separated from stdout . For example, suppose we use the ls -l to display 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 printed output because > redirected the stdout thread of the ls process to otherfile . You can see the redirected output by looking at the otherfile :

 $ cat otherfile -rw-r--r-- 1 wrm staff 0 6 Nov 20:44 myfile $ 

But > did not redirect the stderr stream. You can verify this by removing myfile and restarting the ls -l redirected ls -l :

 $ rm myfile $ ls -l myfile > otherfile ls: myfile: No such file or directory $ 

So you can see that although stdout was redirected to otherfile , stderr not redirected and therefore its contents appeared on the terminal. Also note that otherfile now empty because the ls did not find myfile and therefore there was nothing to send to stdout .

You can also redirect stderr , but it depends on your shell (the program that controls your terminal) how it is done.

+4
source

If you need to print a specific output stream, use fprintf.

If you need to show an error message, use fprintf w / stderr

If you are developing a command line executable and just want to display something to the user, use printf.

+1
source

Source: https://habr.com/ru/post/1444494/


All Articles