Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is preferable - printf or fprintf

Tags:

c

output

printf

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?

like image 425
Lior Avatar asked Nov 07 '12 01:11

Lior


People also ask

Is fprintf better than printf?

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.

Is printf same as fprintf?

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.

What is the difference between printf sprintf and fprintf?

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.

Where is fprintf used?

fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...);


2 Answers

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.

like image 134
Daniel Fischer Avatar answered Sep 24 '22 10:09

Daniel Fischer


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.

like image 41
William Morris Avatar answered Sep 25 '22 10:09

William Morris