tags:

views:

160

answers:

6

in c printf() returns what?

+1  A: 

It returns the number of characters printed. See man fprintf for simple questions like this.

From the man page:

These functions return the number of characters printed (not including the trailing '\0' used to end output to strings) or a negative value if an output error occurs, except for snprintf() and vsnprintf(), which return the number of characters that would have been printed if the n were unlimited (again, not including the final '\0').

Paul R
+4  A: 

printf returns:

  • On success, the total number of characters written.
  • On failure, a negative number.
codaddict
+1  A: 

Number of characters (not including the trailing \0) printed on success, negative value on failure. see man printf.

Jasmeet
Don't count on it returning `-1` on failure; it can return any negative value.
Arkku
Thats correct, I will fix the answer. Thanks
Jasmeet
Note that printf() does not print the trailing null - which is why it is not included in the count.
Jonathan Leffler
+1  A: 

The result of "printf" is the number of characters written. If a write error occurs, "printf" returns a negative number. (ANSI standard)

Even wikipedia has a whole article about printf, where you can find the different return values for different languages and times.

tanascius
+1  A: 

Exactly what it says it returns in any decent library reference

On success, the total number of characters written is returned. On failure, a negative number is returned.

Clifford
+1  A: 
  1. Point your browser to www.google.com;
  2. Search "printf c";
  3. Almost any result you'll get will tell you:

Return Value

On success, the total number of characters written is returned.
On failure, a negative number is returned.

Was that so difficult?

Matteo Italia
Which will get you common practice in almost all cases, which is very different from asking what it actually returns.
David Thornley
This is the best answer. For any questions as trivial as "what does C function x return", the above should be your first resource. Leave SO for more "interesting" problems and questions. Futhermore, reading a paragraph or two worth of documentation to get an answer is genereally time well spent (I call it colateral learning).
NealB
I don't understand the downvotes. I provided exactly the same information provided by all the other answers, **including the one with three upvotes** and the information needed to find that answer without spamming SO; I don't even think I've been impolite.NealB expressed perfectly my position on the topic; I like to give complete and insightful answers to serious/interesting questions, but for such things teaching to use Google is the best thing you can do. In other forums in which I often write such a question may even have been closed with a (polite) RTFM.
Matteo Italia
@Matteo: I agree with you - your answer is a polite RTFM and totally valid for this question ... +1
tanascius
Thank you tanascius. :)
Matteo Italia