19

Today in my interview, the interviewer asked: printf is a function and every function returns something; int, void, float, etc. Now what does printf return as it's a function?

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    Whether a `void` function "does not return anything" or "returns `void`" is a matter of linguistic preference. – AnT stands with Russia May 16 '10 at 20:28
  • 21
    This is a very bad interview question: nobody should be supposed to remember the details of any library, documentations exist for that! This means you might be somewhat lucky if you don't get the job. – o0'. May 16 '10 at 20:31
  • 4
    but i got the job and i accepted the offer letter.. and i dont know its good or bad but its Adobe. I am 2009 passout and quite happy to have a job in that company..:-) –  May 16 '10 at 20:35
  • @GMan - Save the Unicorns thank you very much... –  May 16 '10 at 20:43
  • 8
    This is a duplicate of http://stackoverflow.com/questions/2727922/in-c-printf-returns-what. However it is inefficient to ask on a forum rather that consult the library reference. If you did not know in the interview, "I would check the library documentation" would have been your best answer (I am not sure "I'll ask on Stack Overflow" would get the best reception). In an interview the best thing is not to panic or look flustered when faced with a question you cannot answer; say how you'd go about finding the answer or solving the problem - that's what companies want; problem solvers. – Clifford May 16 '10 at 21:48
  • @GregS i think u are asking question from a wrong guy.. I think u should ask this question to 4 guys (who took my four different tech rounds..) :-P –  May 17 '10 at 09:28

4 Answers4

37

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

See reference here

brickner
  • 6,595
  • 3
  • 41
  • 54
8

Not every function returns something, which is indicated by using void:

void function_returns_nothing(void);

printf is a function (declared in <stdio.h>) and it returns an int, which is the number of characters outputted. If an error occurs, the number is negative.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
1

printf()'s reference from MSDN:

Returns the number of characters printed, or a negative value if an error occurs.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
1

To add a detail refinement to other fine answers:

printf() returns an int, yet does that indicate transmitted vs. printed/written characters?

The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred. C11dr §7.21.6.3 3 (my emphasis)

On success, the number transmitted is returned. stdout is typically buffered, so the number of characters printed may not be realized or fail until later.

When int printf() has trouble for various reasons, it returns a negative number. The number of characters transmitted is not known.

If a following successful fflush(stdout) occurs, then the non-negative value from printf() is certainly the number printed.

int transmitted = printf(......);
int flush_retval = fflush(stdout);

int number_certainly_printed = -1; // Unknown
if (transmitted >= 0 && flush_retval == 0) {
  number_certainly_printed = transmitted;
}

Note that "printing" a '\n' typically flushes stdout, but even that action is not specified.
What are the rules of automatic flushing stdout buffer in C?

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • when this nuance may be important? – Nusrat Nuriyev Oct 07 '19 at 17:19
  • @CEOatApartico Commonly before user input. – chux - Reinstate Monica Oct 07 '19 at 19:16
  • Could you provide a bad case when this nuance may be important? "When int printf() has trouble for various reasons," Could you show the real example where the trouble occure for certain reason? Thank you! I have – Nusrat Nuriyev Oct 08 '19 at 10:07
  • When I imitate an error printf(3) between fflush and number_certainly_printed declaration and definition and then I have tried to output the value of number_certainly_printed : printf("%d", number_certainly_printed); I have got nothing as an output for the latter printf, and program returned some garbage value as well... that's why I am asking... – Nusrat Nuriyev Oct 08 '19 at 10:09