What value does a call to printf() produce?
In my programming class, I don't understand what the value of printf("World")
is (question my prof is asking)? What value does printf
return? He says it i开发者_高级运维s 5, but I don't know why.
Because printf()
returns the number of bytes that were output. For "World", that's 5: 5 characters, 5 bytes. Count 'em...
When you're learning a new language, it's always a good idea to read the documentation for the new functions you're learning how to use. If you follow the link above, you'll find the following documented for the return value of printf():
Upon successful completion, the fprintf() and printf() functions shall return the number of bytes transmitted.
If your professor is asking you this question, you'll probably find that this was documented in the class textbook as well. Read it, learn it, love it!
I guess you are talking about C or C++? Then here you will find the answer:
On success, the total number of characters written is returned.
Remember... these are different things: What will printf PRINT. What will printf RETURN.
The function will RETURN "the number of bytes" that it output.
It might PRINT the word "duck" on your screen. ... but it will RETURN the number 4. (It wrote 4 characters.)
精彩评论