2

I have 3 different processes that all print out single characters using printf. But I can't see them in the terminal. When I add a newline, printf("\n H") so each character is on a new line, I can see them. Why doesn't it work without the newline character?

bitmask
  • 32,434
  • 14
  • 99
  • 159
DoobyScooby
  • 367
  • 3
  • 6
  • 17

3 Answers3

5

Its a matter of flushing. If you flush the buffers after each printf, you should get output closer to what you want. To flush the standard output simply do fflush( stdout ).

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 1
    Also, output is usually buffered until a newline or a certain amount of data in the buffer is reached. That's why you didn't need to flush when using a newline. – UncleO Sep 26 '11 at 20:16
3

The C standard defines 3 types of buffering for output streams:

  • Unbuffered → no buffering done
  • Line-buffered → buffer until newline seen
  • Fully-bufferd → buffer up to the buffer size

An output stream's buffering type can be changed via the setvbuf(3) and setbuf(3) functions.

The C standard requires stderr to not be fully-buffered at startup (it is usually unbuffered on many implementations, so as to see errors as soon as posible); and stdout to be fully-buffered only if it can be determined to not refer to a terminal (when it refers to a terminal, many implementations initialize it as line-buffered, which is what you are seeing).

ninjalj
  • 42,493
  • 9
  • 106
  • 148
-1

use'write(1,&c,1)' system call, or

fprintf(stderr,'%c', c);
  • 1
    `stderr` usually points to `stdout` and is flushed after every write, however it could be piped to point to any other file (different that the one `stdout` points to) so this is not a valid solution. – K-ballo Sep 26 '11 at 22:09