views:

82

answers:

3

In my programming class, I don't understand what the value of printf("World") is (question my prof is asking)? What value does a printf have? He says it is 5, but I don't know why. Thanks.

+5  A: 

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!

Shog9
Excellent idea, **read** the documentation. Now I'm wrestling with the idea of upvoting the question. I think it was clear and useful, even if the OP hasn't *engaged* with the idea of SO in the last 2 months.
pavium
@pavium: Are you considering the user rather than content for voting? For shame. :)
Roger Pate
Yes, I know. I try to resist, but sometimes I fail. Mea Culpa.
pavium
+2  A: 

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.

tangens
+1  A: 

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.)

Susanna