Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printf return a value?

Tags:

c

printf

I know that printf returns a negative error or number of characters printed on success. Only reason to check this return value is if the execution of program somehow depends on the printf status. But I could not think of a scenario where this return value is useful. I, for one, have never checked the return value of printf function.

What are the scenarios where return status from printf is useful?

like image 610
Rumple Stiltskin Avatar asked Nov 23 '12 20:11

Rumple Stiltskin


People also ask

Does printf return a value?

The fprintf() , printf() , and sprintf() functions return the number of characters output, or a negative value if an output error occurs. The ending null character is not counted.

What value is returned by printf () and scanf () functions?

printf() - printf() returns the number of characters successfully written on the output. It is used to simply print data in the output. scanf() - It returns the number of data items that have been entered successfully.

What is the value of printf?

printf() returns total no. of character printed on console, you pass 1000; so first inner printf() function will work and print 1000,and here no. of character is 4.

Does scanf return value?

scanf returns EOF if end of file (or an input error) occurs before any values are stored. If any values are stored, it returns the number of items stored; that is, it returns the number of times a value is assigned by one of the scanf argument pointers.


1 Answers

There are at least 2 uses of the return value of printf:

  1. To check for errors. This is rarely important when your output is interactive or purely informative, but for programs that are processing text from stdio or a file and writing the result to stdout, it's important to know whether an error (such as disk full, closed pipe, or dropped network connection) prevented the entire output from being written. This is especially important if the source file will be deleted/replaced by the output once the output is complete. However, due to buffering, checking the result of printf itself is usually not helpful. You'll want to check ferror(stdout) after writing the last output and flushing in order to get a reliable indication of whether any write errors occurred. On the other hand, checking the return value of printf allows you to detect failure early so you don't waste time attempting to write millions of lines when the first line already failed, so it can be a very useful check; it's just not a sufficient check by itself.

  2. If the number of characters output is not obvious, you may want to know how many characters were written. A naive usage of this number would be for lining up columns, but assuming characters have fixed width in their visual presentation is rather antiquated. It might make sense if you were formatting source code, though. Another similar usage (not sure whether this is less or more antiquated) is writing fixed-width fields in a file. For example, if your fields are 80 bytes and printf returned 52, you'd want to write 28 more padding bytes to finish off the field.

like image 151
R.. GitHub STOP HELPING ICE Avatar answered Sep 22 '22 00:09

R.. GitHub STOP HELPING ICE