0

I've written a simple C program that is meant to pipe stdin to stdout without buffering characters:

#include <stdio.h>

int main (int argc, char *argv[])
{
  int c;

  while ((c = getchar()) != -1) {
    putchar(c);
    fflush(stdout);
  }

  return 0;
}

However, this program still seems to be buffering characters until newlines, despite the call to fflush. Why is this happening and how can I fix it (if possible)?

Ajay Tatachar
  • 383
  • 3
  • 16

1 Answers1

1

In this case, the buffering is done by the terminal driver which doesn't send the input to your program until you press ENTER. So your program doesn't even get any characters and fflush() isn't even called. It's just waiting for input at the getchar() call.

P.P
  • 117,907
  • 20
  • 175
  • 238