0

I cannot figure out why program control does not reach the third printf, right after the for loop.

Why won't the third printf print?

If I change the for loop to while loop, it still will not print.

Here is the program and output:

main()
{
    double nc;

    printf ("Why does this work, nc = %f\n", nc);
    for (nc = 0; getchar() != EOF; ++nc)
    {
        printf ("%.0f\n", nc);
    }
    printf ("Why does this work, nc = %f", nc);
}

The output is:

Why does this work, nc = 0.000000
test
0
1
2
3
4
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Chris Harding
  • 27
  • 1
  • 8

2 Answers2

4

It works fine for me, how are you trying to termintate the program? The for-loop should end once EOF is detected as input by getchar().

EOF is Control-Z (^Z) under Windows and Control-D (^D) under Linux/Unix. Once I enter this, the loop terminates and I get the final printf() to display its output.

As a final note (as mentioned by @DanielFisher too), add a '\n' at the end of your final printf() call as it may be required by your particular implementation or otherwise the program's behavior might be undefined (thanks to @KeithThompson and @AndreyT pointing this out in the comments):

 printf ("Why does this work, nc = %f\n", nc);
Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    The trailing `\n` isn't just stylistic. It's implementation-defined whether a trailing `'\n'` is *required*. If it is required, failing to provide one makes the program's behavior undefined. – Keith Thompson Jun 30 '12 at 21:37
  • 1
    @KeithThompson Interesting, could you elaborate on this requirement, I hadn't heard about this before. Does it have to do with whether it causes the output buffer to be flushed? – Levon Jun 30 '12 at 21:38
  • 1
    @Levon: The language specification states in 7.19.2/2 with regard to text streams: "Whether the last line requires a terminating new-line character is implementation-defined." – AnT stands with Russia Jun 30 '12 at 21:44
  • Why the downvote? I'd be happy to improve the answer or correct any errors, but It's not useful to OP, SO or me without an explanation. – Levon Jun 30 '12 at 22:07
  • @AndreyT Thanks .. I just found it and read up on it. Eliminated all references to stylistic :) – Levon Jun 30 '12 at 22:12
  • @Simon Thanks .. that's what I surmised too after KeithThompson's comment. – Levon Jun 30 '12 at 22:40
  • @Simon: On many implementations, the buffer will be properly flushed whether it's terminated with a `\n` or not. On others, it might not be. It's a good idea in any case (unless you have some specific reason not to terminate the stream with a `\n` *and* you know how your implementation behaves). – Keith Thompson Jun 30 '12 at 23:27
  • C requires the stream to be flushed, but the UB from a text file ending without a newline trumps that. On Windows, that is. POSIX requires text mode to behave identically to binary mode, which means there's no UB invoked on POSIX systems. – R.. GitHub STOP HELPING ICE Jul 01 '12 at 04:12
0

printf is buffered, that's why the final line may not be displayed. That means a call to printf may not result in a direct output as the function accumulates data before putting it in the output (your terminal).

A call to fflush after your last printf will put everything that remains in the buffer in your terminal. Also, the buffer is flushed every time you ask for a newline.

Simon
  • 860
  • 7
  • 23