2

I know this is true for C++:

         buffering  output

cerr:    unbuffered stderr
clog:    buffered   stderr
cout:    buffered   stdout

In C equivalents for cerr and cout are:

cerr:    fprintf(stderr, ...)
cout:    fprintf(stdout, ...) or printf(...)

Is there an equivalent for clog?

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
  • `cout` is buffered, but see "http://stackoverflow.com/questions/3723795/is-stdout-line-buffered-unbuffered-or-indeterminate-by-default " as buffering doesn't work exactly the same in C and C++. I'm not sure that adding a buffer to `stderr` is a good idea, since then you have to remember to `fflush` it when writing actual errors, or fatal ones won't make it out. – Potatoswatter Jun 10 '14 at 09:08
  • @Potatoswatter thanks, I changed cout to buffered. I would never use clog for actual errors, so it's not a problem if it's not flushed. – Babken Vardanyan Jun 10 '14 at 09:23
  • That doesn't make sense. C++ `clog` and `cout` are always buffered, but the question is about C `stderr`. If you change it to buffered, then you have no unbuffered error output facility in C. – Potatoswatter Jun 10 '14 at 13:49
  • @Potatoswatter You are right, I was thinking about C++. – Babken Vardanyan Jun 10 '14 at 17:15

2 Answers2

2

Yes, using setvbuf and full buffering (_IOFBF):

Full buffering: On output, data is written once the buffer is full (or flushed). On Input, the buffer is filled when an input operation is requested and the buffer is empty.

#include <stdio.h>

int main(void)
{
    char buff[BUFSIZ];

    setvbuf(stderr, buff, _IOFBF, BUFSIZ);
    fprintf(stderr, "Hello world\n");
    getchar();
    fflush(stderr);
    return 0;
}

The value of BUFSIZ is chosen on each system so as to make stream I/O efficient. So it is a good idea to use BUFSIZ as the size for the buffer when you call setvbuf.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

No, C has only stderr and stdout and standard output streams. But you can set the buffering on stderr as you please with setvbuf.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836